如何在 UWP 中获取 CollectionViewSource.View.Count 的更新
How to get an update on CollectionViewSource.View.Count in UWP
我正在使用 FileInformationFactory.GetVirtualizedFilesVector()
检索文件夹中大约 5k 个文件的 FileInformation.DisplayName
属性,以通过 CollectionViewSource
在 UWP 的 GridView 中显示它们。虽然显示有效,但我无法获得已检索项目的正确计数。
我知道我可以通过等待 GetFilesAsync
和计数来做到这一点,但这会破坏使用虚拟化向量的速度目的,因此我想避免它。
这是XAML
xmlns:ba="using:Windows.Storage.BulkAccess"
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Margin="16">
<StackPanel Orientation="Horizontal">
<AppBarButton Icon="Folder" Click="AppBarButton_Click" LabelPosition="Collapsed"/>
<AppBarButton Icon="Refresh" Click="RefreshAppBarButton_Click" LabelPosition="Collapsed"/>
</StackPanel>
<TextBlock x:Name="infoTextBlock" FontWeight="Medium"/>
</StackPanel>
<GridView Grid.Row="1" Grid.Column="1"
ItemsSource="{x:Bind FileCollectionViewSource.View, Mode=OneWay}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="ba:FileInformation">
<TextBlock Text="{x:Bind DisplayName}"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
以及后面的代码
private StorageFolder _folder;
public CollectionViewSource FileCollectionViewSource { get; set; } = new CollectionViewSource();
private async void AppBarButton_Click(object sender, RoutedEventArgs e)
{
var folderPicker = new FolderPicker
{
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
ViewMode = PickerViewMode.Thumbnail
};
folderPicker.FileTypeFilter.Add("*");
_folder = await folderPicker.PickSingleFolderAsync();
if (_folder != null)
{
GetFileVectors();
}
else
{
infoTextBlock.Text = "Pick folder";
}
}
private void GetFileVectors()
{
var queryOptions = new QueryOptions
{
FolderDepth = FolderDepth.Deep,
IndexerOption = IndexerOption.UseIndexerWhenAvailable
};
var fileQuery = _folder.CreateFileQueryWithOptions(queryOptions);
var fileInformationFactory = new FileInformationFactory(fileQuery, ThumbnailMode.SingleItem, Constants.Thumbnail_Size,
ThumbnailOptions.UseCurrentScale, false);
var filesVector = fileInformationFactory.GetVirtualizedFilesVector();
FileCollectionViewSource.Source = filesVector;
var count = FileCollectionViewSource.View.Count;
infoTextBlock.Text = $"View count {count}";//Returns 0
}
private void RefreshAppBarButton_Click(object sender, RoutedEventArgs e)
{
infoTextBlock.Text = $"View count {FileCollectionViewSource.View.Count}";//returns the correct count
}
结果:
当我选择一个文件夹时,调用 GetFileVectors() 方法,它执行批量搜索并设置 FileCollectionViewSource.Source = filesVector;
,并且 FileCollectionViewSource.View
是 OneWay
绑定到 GridView
。但是,FileCollectionViewSource.View.Count
returns 0现阶段。
如果现在我单击刷新按钮 RefreshAppBarButton_Click
,则会返回准确的计数。
我可以想象 FileCollectionViewSource.View
需要一些时间来解决,确实在打印出 FileCollectionViewSource.View.Count
之前设置 await Task.Delay(1000)
是可行的,但我正在寻找更新 await Task.Delay(1000)
的方法=26=] 当 FileCollectionViewSource.View
完成时。
有没有办法通过某些事件自动执行此操作?
谢谢!
关注 Nico Zhu - MSFT,在设置源后触发一个事件FileCollectionViewSource.View.VectorChanged += View_VectorChanged;
然后
private void View_VectorChanged(IObservableVector<object> sender, IVectorChangedEventArgs @event)
{
if (@event.CollectionChange == CollectionChange.Reset)
{
infoTextBlock.Text = $"{FileCollectionViewSource.View.Count} ";//returns the correct count
}
}
我正在使用 FileInformationFactory.GetVirtualizedFilesVector()
检索文件夹中大约 5k 个文件的 FileInformation.DisplayName
属性,以通过 CollectionViewSource
在 UWP 的 GridView 中显示它们。虽然显示有效,但我无法获得已检索项目的正确计数。
我知道我可以通过等待 GetFilesAsync
和计数来做到这一点,但这会破坏使用虚拟化向量的速度目的,因此我想避免它。
这是XAML
xmlns:ba="using:Windows.Storage.BulkAccess"
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Margin="16">
<StackPanel Orientation="Horizontal">
<AppBarButton Icon="Folder" Click="AppBarButton_Click" LabelPosition="Collapsed"/>
<AppBarButton Icon="Refresh" Click="RefreshAppBarButton_Click" LabelPosition="Collapsed"/>
</StackPanel>
<TextBlock x:Name="infoTextBlock" FontWeight="Medium"/>
</StackPanel>
<GridView Grid.Row="1" Grid.Column="1"
ItemsSource="{x:Bind FileCollectionViewSource.View, Mode=OneWay}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="ba:FileInformation">
<TextBlock Text="{x:Bind DisplayName}"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
以及后面的代码
private StorageFolder _folder;
public CollectionViewSource FileCollectionViewSource { get; set; } = new CollectionViewSource();
private async void AppBarButton_Click(object sender, RoutedEventArgs e)
{
var folderPicker = new FolderPicker
{
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
ViewMode = PickerViewMode.Thumbnail
};
folderPicker.FileTypeFilter.Add("*");
_folder = await folderPicker.PickSingleFolderAsync();
if (_folder != null)
{
GetFileVectors();
}
else
{
infoTextBlock.Text = "Pick folder";
}
}
private void GetFileVectors()
{
var queryOptions = new QueryOptions
{
FolderDepth = FolderDepth.Deep,
IndexerOption = IndexerOption.UseIndexerWhenAvailable
};
var fileQuery = _folder.CreateFileQueryWithOptions(queryOptions);
var fileInformationFactory = new FileInformationFactory(fileQuery, ThumbnailMode.SingleItem, Constants.Thumbnail_Size,
ThumbnailOptions.UseCurrentScale, false);
var filesVector = fileInformationFactory.GetVirtualizedFilesVector();
FileCollectionViewSource.Source = filesVector;
var count = FileCollectionViewSource.View.Count;
infoTextBlock.Text = $"View count {count}";//Returns 0
}
private void RefreshAppBarButton_Click(object sender, RoutedEventArgs e)
{
infoTextBlock.Text = $"View count {FileCollectionViewSource.View.Count}";//returns the correct count
}
结果:
当我选择一个文件夹时,调用 GetFileVectors() 方法,它执行批量搜索并设置 FileCollectionViewSource.Source = filesVector;
,并且 FileCollectionViewSource.View
是 OneWay
绑定到 GridView
。但是,FileCollectionViewSource.View.Count
returns 0现阶段。
如果现在我单击刷新按钮 RefreshAppBarButton_Click
,则会返回准确的计数。
我可以想象 FileCollectionViewSource.View
需要一些时间来解决,确实在打印出 FileCollectionViewSource.View.Count
之前设置 await Task.Delay(1000)
是可行的,但我正在寻找更新 await Task.Delay(1000)
的方法=26=] 当 FileCollectionViewSource.View
完成时。
有没有办法通过某些事件自动执行此操作? 谢谢!
关注 Nico Zhu - MSFT,在设置源后触发一个事件FileCollectionViewSource.View.VectorChanged += View_VectorChanged;
然后
private void View_VectorChanged(IObservableVector<object> sender, IVectorChangedEventArgs @event)
{
if (@event.CollectionChange == CollectionChange.Reset)
{
infoTextBlock.Text = $"{FileCollectionViewSource.View.Count} ";//returns the correct count
}
}