好的所以我有一个简单的uwp应用程序,我试图从 KnownFolders.VideoLibrary 获取数据并使其更快我使用索引与 QueryOptions 即可。奇怪的是,在第一次加载页面时,我没有收到任何错误,但是当我尝试钻入另一个文件夹并再次导航到同一页面时,我得到了这个 Com Exception
我的ViewModel中的错误的类型为System.Runtime.InteropServices.COMException
错误HRESULT E_FAIL已从调用COM组件返回
private async Task FillUpFolders()
{
uint index = 0, stepSize = 5;
//on the first load of the LibraryPage I dont get any exception
//but on the 2nd load, I get exception on the line below.
var VideoQuery = FileHelper.GetVideoFoldersQuery(MainFolder, 200);
IReadOnlyList<StorageFolder> folders = await VideoQuery.GetFoldersAsync(index, stepSize);
index += 5;
while (folders.Count != 0)
{
var folderTask = VideoQuery.GetFoldersAsync(index, stepSize).AsTask();
foreach (StorageFolder folder in folders)
{
var vv = new Folder
{
MyStorageFolder = folder,
Title = folder.DisplayName,
Thumbnail = new BitmapImage(new Uri("ms-appx:///Assets/FolderIcon.png")),
MyStretch = Windows.UI.Xaml.Media.Stretch.Uniform
};
Source.Add(vv);
}
folders = await folderTask;
index += 5;
}
}
视频文件查询方法
internal static StorageFolderQueryResult GetVideoFoldersQuery(StorageFolder Folder, uint thumbnailRequestedSize)
{
if (videoFolderOptions is null)
{
videoFolderOptions = new QueryOptions(CommonFolderQuery.DefaultQuery)
{
IndexerOption = IndexerOption.OnlyUseIndexerAndOptimizeForIndexedProperties//check sort order later
};
videoFolderOptions.SetThumbnailPrefetch(ThumbnailMode.VideosView, thumbnailRequestedSize, ThumbnailOptions.UseCurrentScale);
}
return Folder.CreateFolderQueryWithOptions(videoFolderOptions);
}
我在github仓库上提供我的项目,这样任何人都可以轻松地重现问题并轻松分析它。
https://github.com/touseefbsb/UWPStorageFolderBug
栈跟踪
&#34; at Windows.Storage.StorageFolder.CreateFolderQueryWithOptions(QueryOptions queryOptions)\ r \ n在Fluent_Video_Player.Helpers.FileHelper.GetVideoFoldersQuery(StorageFolder Folder,UInt32 thumbnailRequestedSize)\ r \ n在Fluent_Video_Player.ViewModels.LibraryViewModel.d__11.MoveNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n的系统.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification中的\ r \ n (任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.GetResult()\ r \ n在Fluent_Video_Player.ViewModels.LibraryViewModel.d__9.MoveNext()\ r \ n ---从前一个位置的堆栈跟踪结束抛出异常的地方--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在System。 Runtime.CompilerServices.Ta skAwaiter.GetResult()\ r \ n在Fluent_Video_Player.Views.LibraryPage.d__5.MoveNext()&#34;
重现的步骤
注意:我尝试在 LibraryPage.xaml.cs 和 FillUpFolders上使用 Fill()方法执行 ConfigureAwait(false)视图模型中的()和 FillUpFiles()方法会导致 Marshall线程,即使在使用 Dispather helper >之后我也无法解决这个问题强>
答案 0 :(得分:2)
您可以删除选项
IndexerOption = IndexerOption.OnlyUseIndexerAndOptimizeForIndexedProperties
来自函数GetVideoFoldersQuery的,所以它看起来像这个
internal static StorageFolderQueryResult GetVideoFoldersQuery(StorageFolder Folder, uint thumbnailRequestedSize)
{
if (videoFolderOptions is null)
{
videoFolderOptions = new QueryOptions(CommonFolderQuery.DefaultQuery);
videoFolderOptions.SetThumbnailPrefetch(ThumbnailMode.VideosView, thumbnailRequestedSize, ThumbnailOptions.UseCurrentScale);
}
return Folder.CreateFolderQueryWithOptions(videoFolderOptions);
}
使用这个版本,我发现它不会像以前一样出现同样的错误。
我在函数GetDisplayForFile中得到了一个InvalidCastException,但我认为这是一个完全不同的问题。
修改强>
我将以下测试代码块插入代码中的函数OnNavigateTo:
var folder = KnownFolders.VideosLibrary;
// Define two different QueryOptions
var qo_1 = new QueryOptions(CommonFolderQuery.DefaultQuery);
var qo_2 = new QueryOptions(CommonFolderQuery.DefaultQuery)
{
IndexerOption = IndexerOption.OnlyUseIndexerAndOptimizeForIndexedProperties
};
// Query folder and then query the files and subfolders
// using the first QueryOptions qo_1
var fq_1 = folder.CreateFolderQueryWithOptions ( qo_1 ) ;
var fo_1 = await fq_1.GetFoldersAsync ( 0, 5 ) ;
foreach ( var subfold in fo_1 )
{
var f_1 = subfold.CreateFileQueryWithOptions ( qo_1 ) ;
var sf_1 = subfold.CreateFolderQueryWithOptions ( qo_1 ) ;
}
// Query folder and then query the files and subfolders
// using the first QueryOptions qo_2
var fq_2 = folder.CreateFolderQueryWithOptions ( qo_2 ) ;
var fo_2 = await fq_2.GetFoldersAsync ( 0, 5 ) ;
foreach ( var subfold in fo_2 )
{
var f_2 = subfold.CreateFileQueryWithOptions ( qo_2 ) ;
var sf_2 = subfold.CreateFolderQueryWithOptions ( qo_2 ) ;
}
它定义了两个不同的QueryOption对象,然后对每个对象执行两级查询。
第一个工作正常 第二个问题是在第二级查询时崩溃了。
这看起来像是一个你应该向微软报告的错误。您可以在Microsoft论坛中获得更多帮助。