首页 > 解决方案 > C#-UWP FolderPicker 在 Windows 10 的 FolderOpenDialog 上挂起

问题描述

Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();下面的方法行MyPickFolderAsync()应该允许我从默认Downloads文件夹中选择一个文件夹,但是在它打开 Windows 打开文件夹对话框(在Downloads文件夹中)后,对话框挂起(冻结)在那里,我无法从中选择一个子文件夹Downloads文件夹。我在网上看到过类似的问题(例如thisthis),但这些问题似乎与 while 的旧版本有关Windows 10;我正在起诉最新版本1809

那么,我可能在这里遗漏了什么,我们该如何解决呢?这个问题似乎与我没有正确使用有关async/await。我尝试了各种使用方法async/await,但到目前为止没有成功。对使用异步方法有更好理解的人可能会提供更好的帮助。

流程如下:我点击BtnTest调用TestAsync()的按钮依次调用MyPickFolderAsync()。我正在VS2019使用Windows10-ver 1809

private async void BtnTest_Click(object sender, RoutedEventArgs e)
{
    await TestAsync();
}

private async Task TestAsync()
{
    Task<StorageFolder> pickedFolder = MyPickFolderAsync();
    await MyTestAsync(...); //this method does something with pickedFolder folder but that is not relevant to this post since we don't even get to this line in debug mode as the Windows dialog hangs before we get to this line
}

private async Task<StorageFolder> MyPickFolderAsync()
{
    Windows.Storage.Pickers.FolderPicker folderPicker = new Windows.Storage.Pickers.FolderPicker();
    folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads;
    folderPicker.FileTypeFilter.Add("*");

    Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
    if (folder != null)
    {
        // Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
        Windows.Storage.AccessCache.StorageApplicationPermissions.
        FutureAccessList.AddOrReplace("PickedFolderToken", folder);
        //this.textBlock.Text = "Picked folder: " + folder.Name;
    }
    else
    {
        //this.textBlock.Text = "Operation cancelled.";
    }

    return folder;
}

标签: c#uwp

解决方案


首先感谢帮助我解决问题的用户Eldhogive credit where credit's due ,我们应该这样做。

但是,为了这篇文章的其他一些读者的利益,“可能”值得一提的是,当我await在下面的方法代码行上使用时出现以下错误TestAsync()(必须更正):

Task<StorageFolder> pickedFolder = MyPickFolderAsync();

当我await在上述调用中使用时,出现以下错误:

在此处输入图像描述

因此,在阅读了用户的响应后@Eldho,我不得不Task<...>从上面的代码行中删除并添加await到该行(感谢@Eldho)。


推荐阅读