我将以最简单的方式解释我的问题。我一直在处理 listview 和 gridview 以及绑定很长时间,但现在我遇到了一个无法解释的问题,所以我真的需要一些帮助。
下面是我的列表视图的 xaml 代码。
<ListView Name="OtherVideosList" ItemsSource="{x:Bind VideoFiles}" SelectionChanged="OtherVideosList_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:VideoFile">
<StackPanel Orientation="Horizontal">
<Image Source="{x:Bind Thumbnail}"/>
<StackPanel>
<TextBlock Text="{x:Bind FileName}"/>
<TextBlock Text="{x:Bind Duration}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我将它绑定到ObservableCollection 下面是它的数据类型类。
public class VideoFile
{
public string FileName { get; set; }
public string Duration { get; set; }
public StorageFile File { get; set; }
public BitmapImage Thumbnail { get; set; }
}
使用此类我正在创建项目源
public ObservableCollection<VideoFile> VideoFiles { get; set; }
我使用一个按钮打开多个文件,然后将它们放入项目源,以便稍后在媒体元素上播放它们。下面是事件处理程序的代码。
private async void OpenClick(object sender, RoutedEventArgs e)
{
try
{
var p = new FileOpenPicker();
foreach (var item in videoTypes)
{
p.FileTypeFilter.Add(item);
}
//Curentplayingfiles is IReadOnlyList<StorageFiles>
CurrentlyPlayingFiles = await p.PickMultipleFilesAsync();
if (CurrentlyPlayingFiles.Count != 0)
{
if (CurrentlyPlayingFiles.Count == 1)
{ //this if block works absolutely fine
CurrentlyPlayingFile = CurrentlyPlayingFiles[0];
var s = await CurrentlyPlayingFile.OpenReadAsync();
ME.SetSource(s, CurrentlyPlayingFile.ContentType);
}
else
{
VideoFiles = new ObservableCollection<VideoFile>();
foreach (var file in CurrentlyPlayingFiles)
{
//Thumbnail and GetDuration are my own static methods to get thumbnail
//and duration property of the file respectively
VideoFiles.Add(new VideoFile { Thumbnail = await Thumbnail(file), Duration = await GetDuration(file), File = file, FileName = file.DisplayName });
}
//exception occurs on this very line below, because here OtherVideosList has zero items.
OtherVideosList.SelectedIndex = 0;
}
}
}
catch (Exception s){ var dr = s.Message; }
}
我已经在你的评论中提到了关键点。 任何帮助将不胜感激,非常感谢..