Windows 10 IoT Xaml动态添加图像到flipview
本文关键字:图像 flipview 添加 动态 IoT Xaml Windows | 更新日期: 2023-09-27 18:06:44
我有一个文件夹包含在我的项目,其中包含图像。这些图像应该被加载到一个flipview-Control中,它会在3秒后显示它们并显示下一个图像。切换已经工作,但我有问题显示图像。
public MainPage()
{
this.InitializeComponent();
String path = Directory.GetCurrentDirectory() + @"'Imports";
foreach (String imgurl in Directory.GetFiles(path))
{
String filename = Path.GetFileName(imgurl);
Uri uri = new Uri("ms-appx:///" + filename);
var image = new Image
{
Source = new BitmapImage(uri)
};
// fv is my flipview
fv.Items.Add(image);
}
timer = new DispatcherTimer()
{
Interval = TimeSpan.FromSeconds(3)
};
timer.Tick += ChangeImage;
timer.Start();
}
private void ChangeImage(object sender, object o)
{
var totalItems = fv.Items.Count;
var newItemIndex = (fv.SelectedIndex + 1) % totalItems;
fv.SelectedIndex = newItemIndex;
}
我的xhtml代码是这样的:
<FlipView x:Name="fv" HorizontalAlignment="Left" VerticalAlignment="Top" Width="1280" Height="720" SelectionChanged="flipView_SelectionChanged"></flipview>
正如我所说,滑动工作,但图像不显示。我错过了什么?另外,
fv.Items.Count();
总是告诉我,它包含两倍于我在"导入"文件夹中的图像量。为什么?
编辑:private readonly DispatcherTimer timer;
public MainPage()
{
this.InitializeComponent();
String path = Directory.GetCurrentDirectory() + @"'Imports";
fv.ItemsSource = Directory.GetFiles(path);
timer = new DispatcherTimer()
{
Interval = TimeSpan.FromSeconds(3)
};
timer.Tick += ChangeImage;
timer.Start();
}
private void ChangeImage(object sender, object o)
{
var totalItems = fv.Items.Count;
var newItemIndex = (fv.SelectedIndex + 1) % totalItems;
fv.SelectedIndex = newItemIndex;
}
您必须为它添加一个ItemTemplate:
<FlipView x:Name="fv" HorizontalAlignment="Left" VerticalAlignment="Top" Width="1280" Height="720" SelectionChanged="flipView_SelectionChanged">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid>
<Image Source="{Binding PathToImage}" Stretch="UniformToFill" />
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
源代码只需要一个到图像的路径就可以工作。这意味着您可以在代码后面执行此操作:
String path = Directory.GetCurrentDirectory() + @"'Imports";
fv.ItemsSource = Directory.GetFiles(path);
对于fv.Items.Count();
的意外返回值:您是否转储了集合中的项?
foreach(var item in fv.Items.Count()) System.Diagnostics.Debug.WriteLine(item);
或者你可以添加所有的图像到一个新的列表<>,并将该列表设置为ItemsSource
String path = Directory.GetCurrentDirectory() + @"'Imports";
var newSource = new List<Image>();
foreach (String imgurl in Directory.GetFiles(path))
{
String filename = Path.GetFileName(imgurl);
Uri uri = new Uri("ms-appx:///" + filename);
var image = new Image
{
Source = new BitmapImage(uri)
};
// fv is my flipview
newSource.Add(image);
}
fv.ItemsSource = newSource;
当你设置ListView的ItemsSource时,ListView将为每个对象生成一个item。这些项中的每一个都将对象设置为DataContext。当您使用绑定表达式时,您告诉XAML从当前DataContext加载给定属性的值。{Binding PathToIMage}
将从设置为DataContext的对象中获取PathToImage
的值。(作为旁注:指定没有路径,只是写{Binding}
得到整个对象。因此,如果一个字符串被设置为DataContext, {Binding}
将返回该字符串的值。
你可以在这里阅读有关数据绑定。
我认为只需将下面这行粘贴到你的代码中就可以了:
改变这一切…
String path = Directory.GetCurrentDirectory() + @"'Imports";
foreach (String imgurl in Directory.GetFiles(path))
{
String filename = Path.GetFileName(imgurl);
Uri uri = new Uri("ms-appx:///" + filename);
var image = new Image
{
Source = new BitmapImage(uri)
};
// fv is my flipview
fv.Items.Add(image);
}
…这个…
String path = Directory.GetCurrentDirectory() + @"'Imports";
fv.ItemsSource = Directory.GetFiles(path).Select(p => "ms-appx:///" + p);
…这…
<Image Source="{Binding PathToImage}" Stretch="UniformToFill" />
…这个
<Image Source="{Binding}" Stretch="UniformToFill" />