C#-UWP中FolderPicker调用PickSingleFolderAsync弹窗冻结问题求助
Hey there, let's break down why your FolderPicker is freezing when trying to select subfolders in the Downloads directory. The core issue lies in how you're handling async/await in your TestAsync method, which creates UI thread context conflicts that break the picker's functionality.
The Root Cause
In your TestAsync method, you start the MyPickFolderAsync task but don't immediately await it before moving to call MyTestAsync. Even if your debugger never reaches the MyTestAsync line, just launching the picker task without awaiting it creates a race condition with the UI thread. UWP's FolderPicker relies entirely on the UI thread's message loop to work properly—when you let another task run in parallel (even if it's not executing yet), it can block the picker from processing user input.
The Fix
Adjust your TestAsync method to ensure you wait for the picker operation to complete (or be canceled) before proceeding with any other async work. Here's the corrected code:
private async void BtnTest_Click(object sender, RoutedEventArgs e) { await TestAsync(); } private async Task TestAsync() { // Await the folder picker task first to ensure it finishes before moving on StorageFolder pickedFolder = await MyPickFolderAsync(); // Only run MyTestAsync if a folder was successfully picked if (pickedFolder != null) { await MyTestAsync(...); } } 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("*"); // This await properly captures the UI thread context to keep the picker responsive Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync(); if (folder != null) { Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder); //this.textBlock.Text = "Picked folder: " + folder.Name; } else { //this.textBlock.Text = "Operation cancelled."; } return folder; }
Additional Checks to Rule Out Other Issues
- UI Thread Context: Never call
MyPickFolderAsyncfrom a background thread. Since your button click handler is an async void method, it runs on the UI thread by default—but avoid usingConfigureAwait(false)before calling the picker, as that would switch to a non-UI thread and break functionality. - Downloads Folder Size: If your Downloads folder has an extremely large number of files/subfolders, it might cause a temporary delay, but this shouldn't result in a full freeze. Test with a smaller folder to rule this out if the issue persists.
- UWP Permissions: While
PickSingleFolderAsyncdoesn't require special capabilities for the Downloads folder, double-check your app manifest for conflicting permission settings that could restrict folder access.
Why This Works
By awaiting MyPickFolderAsync first, you ensure the FolderPicker runs exclusively on the UI thread without competing tasks. UWP's picker is a modal dialog that needs full access to the UI thread's message loop to process clicks and navigation—splitting or blocking this context is the most common cause of freezes like you're experiencing.
内容的提问来源于stack exchange,提问作者nam

