C# 如何将GridView绑定到XAML中的视频列表?
我有一个要使用XAML绑定到GridView的视频列表。我使用以下代码以编程方式实现了这一点,它正在工作:C# 如何将GridView绑定到XAML中的视频列表?,c#,xaml,C#,Xaml,我有一个要使用XAML绑定到GridView的视频列表。我使用以下代码以编程方式实现了这一点,它正在工作: private async void pageRoot_Loaded(object sender, RoutedEventArgs e) { IList<string> fileTypeFilter = new List<string> { ".avi", ".mkv", ".mp4" }; QueryOptions queryOptions = n
private async void pageRoot_Loaded(object sender, RoutedEventArgs e) {
IList<string> fileTypeFilter = new List<string> { ".avi", ".mkv", ".mp4" };
QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter);
queryOptions.FolderDepth = FolderDepth.Deep;
queryOptions.IndexerOption = IndexerOption.UseIndexerWhenAvailable;
StorageFileQueryResult videoFileQuery = KnownFolders.VideosLibrary.CreateFileQueryWithOptions(queryOptions);
IReadOnlyList<StorageFile> dataSource = await videoFileQuery.GetFilesAsync();
videoGridView.ItemsSource = dataSource; //Yep, that works :)
}
private async void pageRoot\u加载(对象发送方,路由目标){
IList fileTypeFilter=新列表{.avi',.mkv',.mp4};
QueryOptions QueryOptions=新的QueryOptions(CommonFileQuery.OrderByName,fileTypeFilter);
queryOptions.FolderDepth=FolderDepth.Deep;
queryOptions.IndexerOption=IndexerOption.UseIndexerWhen可用;
StorageFileQueryResult videoFileQuery=KnownFolders.VideosLibrary.CreateFileQueryWithOptions(查询选项);
IReadOnlyList dataSource=wait videoFileQuery.getfileasync();
videoGridView.ItemsSource=dataSource;//是的,有效:)
}
不过,我想用XAML进行绑定。因此,我更改了XAML GridView并删除了上述代码的最后一行:
<GridView x:Name="videoGridView" Grid.Row="1" ItemsSource="{Binding dataSource}"/>
只是现在GridView是空的
我必须做些什么才能做到这一点?尝试绑定到ObservableCollection类型的源
this.DataContext = dataSource;
然后将网格更改为此
<GridView x:Name="videoGridView" Grid.Row="1" ItemsSource="{Binding}"/>