我會用最簡單的方式解釋我的問題。 我一直在處理listview和gridview和綁定很長一段時間,但現在我有一個無法解釋的問題,所以我真的需要一些幫助。UWP ListView not loading items
以下是我的listview的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; }
}
我已經在你的評論中提到了關鍵點guyx。 任何幫助將不勝感激,非常感謝..
thankyou先生:) –