AreQueryOptionsSupported等始终为False,求文件夹图片监控最优方案
Hey there! Let's work through this issue you're facing with monitoring image changes in a folder for your UWP app. I'll break down why those AreQueryOptionsSupported and IsCommonFileQuerySupported checks are failing, plus share a more reliable approach for folder monitoring.
为什么会出现返回False的情况?
The key problem here is likely the folder you're adding via RequestAddFolderAsync(). When you add a folder to the Pictures library this way, it might be an external storage location (like an SD card, network share, or a folder outside the app's local storage).
UWP's CommonFileQuery and some advanced QueryOptions rely on the system's indexing service, which doesn't work with external or non-indexed folders. That's why those support checks keep returning False—the folder simply doesn't support those query types.
更可靠的监控方案
Instead of relying on indexed queries, use a combination of basic file queries and folder change notifications. This approach works with almost all folder types (local, external, library-added) and gives you the real-time refresh you need.
步骤1:注册文件夹变化通知
First, after getting your target folder, register for change notifications. This will trigger an event whenever files are added, deleted, or modified in the folder:
// 假设你已经获取到photoFolder var changeToken = await photoFolder.RegisterFolderChangeNotificationAsync(FolderChangeDetectionMode.Default); changeToken.Changed += OnPhotoFolderChanged;
步骤2:处理变化事件并刷新图片列表
In the event handler, you can fetch the latest files, filter for image types, and update your UI:
private async void OnPhotoFolderChanged(IStorageFolderChangeToken sender, object args) { // 获取文件夹内所有文件 var allFiles = await photoFolder.GetFilesAsync(); // 筛选图片格式(可根据需求添加更多格式) var imageFiles = allFiles.Where(file => file.FileType.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || file.FileType.Equals(".png", StringComparison.OrdinalIgnoreCase) || file.FileType.Equals(".jpeg", StringComparison.OrdinalIgnoreCase) || file.FileType.Equals(".bmp", StringComparison.OrdinalIgnoreCase)); // 在UI线程更新你的图片列表 await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { RefreshYourImageListUI(imageFiles); // 替换成你的刷新逻辑 }); }
可选:优化查询(如果文件夹支持)
If you want to use a filtered query directly (and avoid post-filtering), you can check if basic QueryOptions are supported first:
var imageFileTypes = new[] { ".jpg", ".png", ".jpeg", ".bmp" }; var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, imageFileTypes); if (photoFolder.AreQueryOptionsSupported(queryOptions)) { var imageQuery = photoFolder.CreateFileQueryWithOptions(queryOptions); // 注册查询内容变化通知 imageQuery.ContentsChanged += OnImageQueryContentsChanged; // 初始加载图片 var initialImages = await imageQuery.GetFilesAsync(); RefreshYourImageListUI(initialImages); } else { // 回退到基础文件夹通知+手动筛选的方案 var changeToken = await photoFolder.RegisterFolderChangeNotificationAsync(FolderChangeDetectionMode.Default); changeToken.Changed += OnPhotoFolderChanged; }
额外提示
- Don't forget to unregister the change tokens when your page or app is closed to avoid memory leaks.
- If you need background monitoring (even when the app is minimized), consider using a Background Task with the
SystemTriggerType.FileSystemChangetrigger—but note this has some limitations on external storage.
内容的提问来源于stack exchange,提问作者Matthew McDermott

