Listview 与StorageFile.RenameAsync()相比,FileInformation.RenameAsync()会断开绑定
当重命名单击的项时,是什么使以下代码破坏了Listview 与StorageFile.RenameAsync()相比,FileInformation.RenameAsync()会断开绑定,listview,uwp,binding,storagefile,xbind,Listview,Uwp,Binding,Storagefile,Xbind,当重命名单击的项时,是什么使以下代码破坏了列表视图的文件信息数据模板的绑定? 然后,如何获得视图的正确通知,即FileInformation已重命名 考虑一个包含2个图像的文件夹,1.jpg和2.jpg。此文件夹由Windows 10 1909编制索引,UWP应用程序版本为1903 用户选择文件夹后,我们创建一个FileInformationFactory并检索VirtualizedFiles对象,并将其设置为CollectionViewSource的源。后者的视图填充一个ICollection
列表视图的文件信息
数据模板
的绑定?
然后,如何获得视图的正确通知,即FileInformation
已重命名
考虑一个包含2个图像的文件夹,1.jpg和2.jpg。此文件夹由Windows 10 1909编制索引,UWP应用程序版本为1903
用户选择文件夹后,我们创建一个FileInformationFactory
并检索VirtualizedFiles
对象,并将其设置为CollectionViewSource
的源。后者的视图填充一个ICollectionView
,该视图单向绑定到ListView的ItemsSource
我们在列表视图中显示文件信息
的每个文件名称
和文件FolderRelativeId
。
重命名按钮接受ListView。选择EdItem并将其重命名为1.jpg,选项为GenerateUniqueName
目前有两种方法
1) 给定代码的最自然的方法是使用
FileInformation.RenameAsync()
,因为所选项目已经是FileInformation
2) 从folder.GetFileAsync获取Storagefile,其中name参数由FileInformation.name给定,然后调用Storagefile.RenameAsync
在这两种方法中,当我们重命名文件时,listview将按预期进行更新。但是,这不会持续多久,因为我一直在重命名,即使我给了文件重命名的时间。事实上,在第一种情况下
我可以重命名该文件,但是,随着我不断重命名,在某些不清楚的地方,listview似乎停留在以前重命名的名称上,并且与FolderRelativeId
不匹配。例如,文件名显示为“1(2).jpg”,而FolderRelativeId以“1(3).jpg.jpg”结尾。listview无法将该项识别为listview的一部分,因为我无法再选择它,重命名会引发捕获的异常
这个问题似乎没有出现在场景2中,为什么?如何使用场景1(即坚持使用FileInformation
进行重命名)并保持通知处于活动状态以更新UI而不出现此错误
另一个问题是如何在重命名文件后保持选定项不变,因为选定项经常(并非总是!)丢失(索引=-1),可能是因为ICollectionview由于文件系统的通知而被重置
要查看错误,请取消对RenameButtonClick事件中的行的注释(并在上面几行注释storagefile中的重命名调用)。
等待文件信息。重命名同步(“1.jpg”,NameCollisionOption.GenerateUniqueName)代码>
感谢您的帮助,因为几天以来我一直在努力解决这个问题。
谢谢
公共密封部分类场景5:第页,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
public void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
=>PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
公共场景5()
{
this.InitializeComponent();
}
私有ICollectionView _fileCollectionView;
private StorageFolder\u文件夹;
私人查询选项_查询选项;
private StorageFileQueryResult\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)
{
返回;
}
信息。清除();
_文件夹=_pickedFolder;
_查询选项=新查询选项
{
FolderDepth=FolderDepth.Deep,
IndexerOption=IndexerOption.UseIndexerWhen可用,
};
_query=\u folder.CreateFileQueryWithOptions(\u queryOptions);
_fileInformationFactory=新的fileInformationFactory(_查询,ThumbnailMode.SingleItem,160,
ThumbnailOptions.UseCurrentScale,delayLoad:false);
var_vector=_fileInformationFactory.getVirtualizedFileVector();
CollectionViewSource.Source=\u向量;
ItemCollectionView=CollectionViewSource.View;
}
专用静态异步任务PickFolderAsync()
{
var folderPicker=新folderPicker
{
SuggestedStartLocation=PickerLocationId.Desktop,
ViewMode=PickerViewMode.缩略图
};
folderPicker.FileTypeFilter.Add(“*”);
var_pickedFolder=等待folderPicker.PickSingleFolderAsync();
返回_picked文件夹;
}
私有无效列表视图\项目单击(对象发送者,项目单击事件参数e)
{
如果(例如,ClickedItem是文件信息文件信息)
{
添加($“单击{fileInformation.Name}\n{fileInformation.FolderRelativeId}”);
}
}
私有异步无效重命名按钮单击(对象发送方,RoutedEventArgs e)
{
如果(itemCollectionGridView.SelectedItem为文件信息文件信息)
{
添加($”选定项:{fileInformation.Name}\n{fileInformation.FolderRelativeId}”);
尝试
{
var storageFile=await\u folder.GetFileAsync(fileInformation.Name);
等待storageFile.Re
public sealed partial class Scenario5 : Page, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
public Scenario5()
{
this.InitializeComponent();
}
private ICollectionView _fileCollectionView;
private StorageFolder _folder;
private QueryOptions _queryOptions;
private StorageFileQueryResult _query;
private FileInformationFactory _fileInformationFactory;
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)
{
return;
}
Information.Clear();
_folder = _pickedFolder;
_queryOptions = new QueryOptions
{
FolderDepth = FolderDepth.Deep,
IndexerOption = IndexerOption.UseIndexerWhenAvailable,
};
_query = _folder.CreateFileQueryWithOptions(_queryOptions);
_fileInformationFactory = new FileInformationFactory(_query, ThumbnailMode.SingleItem, 160,
ThumbnailOptions.UseCurrentScale, delayLoad: false);
var _vector = _fileInformationFactory.GetVirtualizedFilesVector();
CollectionViewSource.Source = _vector;
ItemCollectionView = CollectionViewSource.View;
}
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 ListView_ItemClick(object sender, ItemClickEventArgs e)
{
if (e.ClickedItem is FileInformation fileInformation)
{
Information.Add($"Click {fileInformation.Name}\n{fileInformation.FolderRelativeId}");
}
}
private async void RenameButton_Click(object sender, RoutedEventArgs e)
{
if (itemCollectionGridView.SelectedItem is FileInformation fileInformation)
{
Information.Add($"Selected item: {fileInformation.Name}\n{fileInformation.FolderRelativeId}");
try
{
var storageFile = await _folder.GetFileAsync(fileInformation.Name);
await storageFile.RenameAsync("1.jpg", NameCollisionOption.GenerateUniqueName);
Information.Add($"Renamed storagefile {storageFile.Name}\n{storageFile.FolderRelativeId}");
//await fileInformation.RenameAsync("1.jpg", NameCollisionOption.GenerateUniqueName);
Information.Add($"Renamed FileInformation result {fileInformation.Name}\n{fileInformation.FolderRelativeId}");
}
catch (Exception ex)
{
Information.Add($"{ex.Message}\n" +
$"{fileInformation.Name}\n{fileInformation.FolderRelativeId}");
}
}
}
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
Information.Clear();
}
}
<Page
x:Class="Virtualization.Scenario5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Virtualization"
xmlns:ba="using:Windows.Storage.BulkAccess"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
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="*"/>
<ColumnDefinition Width="48"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.ColumnSpan="1" Orientation="Horizontal"
BorderBrush="Blue" BorderThickness="0,0,2,2">
<AppBarButton Icon="Folder"
LabelPosition="Collapsed"
Click="FolderPickerButton_Click"/>
<AppBarButton Icon="Rename"
LabelPosition="Collapsed"
Click="RenameButton_Click"/>
<AppBarButton Icon="Clear"
LabelPosition="Collapsed" Label="Select Folder"
Click="ClearButton_Click"/>
</StackPanel>
<ListView x:Name="itemCollectionGridView" Grid.Row="1" Grid.Column="1"
ItemsSource="{x:Bind ItemCollectionView, Mode=OneWay}" IsItemClickEnabled="True"
ItemClick="ListView_ItemClick">
<ListView.ItemTemplate>
<DataTemplate x:DataType="ba:FileInformation">
<StackPanel MinHeight="100">
<TextBlock Text="{Binding Name}" TextWrapping="WrapWholeWords"/>
<TextBlock Text="{Binding Path}" TextWrapping="WrapWholeWords"/>
<TextBlock Text="{Binding FolderRelativeId}" TextWrapping="WrapWholeWords"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView Grid.Row="1" Grid.Column="2" ItemsSource="{x:Bind Information, Mode=OneWay}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel ItemsUpdatingScrollMode="KeepLastItemInView" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>