我的项目中包含一个包含图像的文件夹。这些图像应加载到一个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;}我的XAML代码如下所示:<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);或者,您可以将所有图像添加到新的List 并将该列表设置为ItemsSourceString 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将为每个对象生成一个项目。这些项目中的每一个都将设置对象作为它的DataContext。使用Binding表达式时,您告诉XAML从当前DataContext加载给定Property的。 {Binding PathToIMage}将从设置为DataContext的对象获取PathToImage的值。 (作为一个补充说明:不指定路径,仅编写{Binding}即可获取整个对象。因此,如果将字符串设置为DataContext,则{Binding}将返回字符串的值。)您可以阅读有关DataBinding here的信息。我认为以下行应该通过将其粘贴到您的代码中而起作用:改变这个...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" /> 10-01 10:28