GridView在一个未编入索引的位置松开了对项目的跟踪,许多项目不再显示
我正在为UWP应用程序使用GridView在一个未编入索引的位置松开了对项目的跟踪,许多项目不再显示,gridview,uwp,uwp-xaml,storagefile,Gridview,Uwp,Uwp Xaml,Storagefile,我正在为UWP应用程序使用FileInformationFactory来检索虚拟化的文件向量对象,该对象被转换为FileInformation并显示在GridView中 我想查询插入笔记本电脑的SD卡上的文件夹,并比较GridView在FullyIndexed位置(如我的桌面)时的虚拟化 此SD卡有一个包含15000个零字节文本文件的文件夹。每个文件名为x.txt,其中x从1到15000运行(可以通过单击下面代码中的“添加”按钮在您选择的文件夹中生成此文件;在我的机器上需要一分钟左右,但请记住只
FileInformationFactory
来检索虚拟化的文件向量对象,该对象被转换为FileInformation
并显示在GridView
中
我想查询插入笔记本电脑的SD卡上的文件夹,并比较GridView
在FullyIndexed
位置(如我的桌面)时的虚拟化
此SD卡有一个包含15000个零字节文本文件的文件夹。每个文件名为x.txt
,其中x从1到15000运行(可以通过单击下面代码中的“添加”按钮在您选择的文件夹中生成此文件;在我的机器上需要一分钟左右,但请记住只执行一次!)
结果显示在GridView
中,我在其中显示每个文件的名称
在FullyIndexed
位置,我可以非常快速地上下滚动GridView
,即使使用垂直滚动条也非常快速且不规则,并且GridView
始终能够显示每个文件的名称,尽管会有几秒钟的延迟
在notindex
位置,如SD卡或外部硬盘驱动器,GridView会在我缓慢滚动时显示项目,但在我快速上下滚动时,GridView会松开项目的轨迹,例如,当我将垂直滚动条与我的轨迹板一起使用时。等待无济于事。应用程序仍会响应,但不会显示任何内容。即使向上滚动到顶部,也只显示第一个元素名称,唯一的选择是重新显示整个元素并平滑滚动
如何在notindex
位置解决此问题?我不想一个接一个地将文件检索到内存中,因为这会占用太多时间,而且对于真实照片来说,会太快地消耗用户的RAM。因此使用了getVirtualizedFileVector
我原以为这将是GridView的虚拟化,但事实并非如此,而且该应用程序可以很好地处理17k的真实照片,但只能在FullyIndexed
位置上运行。
多谢各位
<Page
x:Class="Virtualization.Scenario4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ba="using:Windows.Storage.BulkAccess"
xmlns:local="using:Virtualization"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="48"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.ColumnSpan="1" Orientation="Horizontal"
BorderThickness="0,0,2,2">
<AppBarButton x:Name="FolderPickerButton" Icon="Folder"
LabelPosition="Collapsed" Label="Select Folder"
Click="FolderPickerButton_Click"/>
<AppBarButton Icon="ViewAll"
LabelPosition="Collapsed"
Click="ViewAllAppBarButton_Click"/>
<AppBarButton Icon="Add"
LabelPosition="Collapsed"
Click="AddFilesAppBarButton_Click"/>
<AppBarSeparator/>
</StackPanel>
<GridView Grid.Row="1" Grid.Column="1"
ItemsSource="{x:Bind ItemCollectionView, Mode=OneWay}" IsItemClickEnabled="True" ItemClick="GridView_ItemClick">
<GridView.ItemTemplate>
<DataTemplate x:DataType="ba:FileInformation">
<Grid>
<TextBlock MinWidth="80" Text="{Binding Name}" TextWrapping="WrapWholeWords"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<ListView Grid.Row="1" Grid.Column="2" ItemsSource="{x:Bind Information, Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
公共密封部分类场景4:第页,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
public void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
=>PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
公共场景4()
{
this.InitializeComponent();
}
私有ICollectionView _fileCollectionView;
private StorageFolder\u文件夹;
专用IStorageQueryResultBase\u查询;
私有文件信息工厂\u文件信息工厂;
私有对象_向量;
public CollectionViewSource CollectionViewSource{get;set;}=new CollectionViewSource();
公共ICollectionView项目集合视图
{
获取{return\u fileCollectionView;}
设置
{
if(_fileCollectionView!=值)
{
_fileCollectionView=值;
OnPropertyChanged(nameof(ItemCollectionView));
}
}
}
公共ObservableCollection信息{get;private set;}=new ObservableCollection();
私有异步void FolderPickerButton_Click(对象发送方,路由目标)
{
var_pickedFolder=等待PickFolderAsync();
如果(_pickedFolder==null)
{
信息。添加(“选择文件夹”);
返回;
}
添加(“如果尚未完成,则使用添加按钮生成15k文件,否则单击ViewAll按钮”);
_文件夹=_pickedFolder;
}
私有异步无效查询\u ContentsChanged(IStorageQueryResultBase发送方,对象参数)
{
等待CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,async()=>
{
添加($“内容已更改{sender.Folder.Name}{args}”);
});
}
私有void视图_VectorChanged(IObservableVector发送方,IVeCorChangedVentArgs@event)
{
如果(@event.CollectionChange==CollectionChange.Reset)
{
添加($“Vector change{@event.CollectionChange}ItemCount:{sender.Count}索引:{@event.Index}”);
}
}
专用静态异步任务PickFolderAsync()
{
var folderPicker=新folderPicker
{
SuggestedStartLocation=PickerLocationId.Desktop,
ViewMode=PickerViewMode.缩略图
};
folderPicker.FileTypeFilter.Add(“*”);
var_pickedFolder=等待folderPicker.PickSingleFolderAsync();
返回_picked文件夹;
}
私有void GridView\u ItemClick(对象发送者,ItemClickEventArgs e)
{
如果(例如,ClickedItem是文件信息文件信息)
{
添加(fileInformation.Name);
}
其他的
{
如果(如ClickedItem==null)
{
信息。添加(“空”);
}
其他的
{
添加(例如,单击editem.ToString());
}
}
}
private async void AddFilesAppBarButton_Click(对象发送方,RoutedEventArgs e)
{
对于(int i=1;我怀疑这种行为与查询没有索引的文件时性能不佳有关。我将尝试确认这一点。@RoyLi MSFT,请注意SD卡是一种现代的SD卡,我也在外部硬盘上试用过它。文件实际上加载得很快,但滚动会导致全部失败。这就是为什么我选择0字节f艾利斯,让苏
public sealed partial class Scenario4 : Page, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
public Scenario4()
{
this.InitializeComponent();
}
private ICollectionView _fileCollectionView;
private StorageFolder _folder;
private IStorageQueryResultBase _query;
private FileInformationFactory _fileInformationFactory;
private object _vector;
public CollectionViewSource CollectionViewSource { get; set; } = new CollectionViewSource();
public ICollectionView ItemCollectionView
{
get { return _fileCollectionView; }
set
{
if (_fileCollectionView != value)
{
_fileCollectionView = value;
OnPropertyChanged(nameof(ItemCollectionView));
}
}
}
public ObservableCollection<string> Information { get; private set; } = new ObservableCollection<string>();
private async void FolderPickerButton_Click(object sender, RoutedEventArgs e)
{
var _pickedFolder = await PickFolderAsync();
if (_pickedFolder == null)
{
Information.Add("Choose a folder");
return;
}
Information.Add("Generate 15k files with the Add button if not done already, otherwise click the ViewAll button");
_folder = _pickedFolder;
}
private async void Query_ContentsChanged(IStorageQueryResultBase sender, object args)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
Information.Add($"Contents Changed {sender.Folder.Name} {args}");
});
}
private void View_VectorChanged(IObservableVector<object> sender, IVectorChangedEventArgs @event)
{
if (@event.CollectionChange == CollectionChange.Reset)
{
Information.Add($"Vector Changed {@event.CollectionChange} ItemCount: {sender.Count} Index: {@event.Index}");
}
}
private static async Task<StorageFolder> PickFolderAsync()
{
var folderPicker = new FolderPicker
{
SuggestedStartLocation = PickerLocationId.Desktop,
ViewMode = PickerViewMode.Thumbnail
};
folderPicker.FileTypeFilter.Add("*");
var _pickedFolder = await folderPicker.PickSingleFolderAsync();
return _pickedFolder;
}
private void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
if (e.ClickedItem is FileInformation fileInformation)
{
Information.Add(fileInformation.Name);
}
else
{
if (e.ClickedItem == null)
{
Information.Add("Null");
}
else
{
Information.Add(e.ClickedItem.ToString());
}
}
}
private async void AddFilesAppBarButton_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i <= 15000; i++)
{
await _folder.CreateFileAsync(i + ".txt", CreationCollisionOption.ReplaceExisting);
if (i % 250 == 0)
{
Information.Add(i.ToString());
}
}
}
private async void ViewAllAppBarButton_Click(object sender, RoutedEventArgs e)
{
if (_folder == null)
{
Information.Add("Choose folder first");
return;
}
var state = await _folder.GetIndexedStateAsync();
var queryOptions = new QueryOptions { FolderDepth = FolderDepth.Deep, IndexerOption = IndexerOption.UseIndexerWhenAvailable };
Information.Add($"{_folder.Name} is {state} {queryOptions.IndexerOption}");
_query = _folder.CreateFileQueryWithOptions(queryOptions);
var st = Stopwatch.StartNew();
var numOfFiles = (await _query.GetItemCountAsync());
st.Stop();
Information.Add($"{state} {queryOptions.IndexerOption}. Found {numOfFiles} in {st.ElapsedMilliseconds / 1000.0}");
if (numOfFiles==0)
{
Information.Add("Generate files by clicking the Add button");
return;
}
_fileInformationFactory = new FileInformationFactory(_query, ThumbnailMode.SingleItem, Constants.Thumbnail_Size,
ThumbnailOptions.UseCurrentScale, delayLoad: false);
_vector = _fileInformationFactory.GetVirtualizedFilesVector();
CollectionViewSource.Source = _vector;
ItemCollectionView = CollectionViewSource.View;
_query.ContentsChanged += Query_ContentsChanged;
CollectionViewSource.View.VectorChanged += View_VectorChanged;
}
}