question

XiaoLi8848-5499 avatar image
0 Votes"
XiaoLi8848-5499 asked RoyLi-MSFT commented

UWP (C#) program freezes after the path selector is displayed

I referenced Microsoft Docs and wrote a piece of C# code to make the program display a path selector, but VS showed an error message: await can only be used for asynchronous methods, refer to the use of async mark..., but this method is awaitable and has async mark. After trying to compile, there was an error message: "CompileXaml" task returned false, but no error was recorded.

After I changed it to ".AsTask().Result", the program can display the path picker, but the program freezes after it is displayed. Neither the main interface nor the path picker responds (but not the unresponsive state), please ask How to solve the problem?

c#
var folderPicker = new Windows.Storage.Pickers.FolderPicker
            {
                SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop
            };
            Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync(); ->ERROR
            if (folder != null)
            {
                pass
            }
            else
            {
                pass
            }


113489-image.png

c#
var folderPicker = new Windows.Storage.Pickers.FolderPicker
            {
                SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop
            };
            Windows.Storage.StorageFolder folder = folderPicker.PickSingleFolderAsync().AsTask().Result;
            if (folder != null)
            {
                pass
            }
            else
            {
                pass
            }



windows-uwp
image.png (143.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@XiaoLi8848-5499 Any updates about your issue? Have you solved your issue?

0 Votes 0 ·

1 Answer

AryaDing-MSFT avatar image
0 Votes"
AryaDing-MSFT answered

Hi,

Welcome to Microsoft Q&A!

You need to include async keyword in the method declaration of any method in which you use the await operator. Please refer to the following code.

 public async void PickerFolder() {
 …
 StorageFolder folder = await folderPicker.PickSingleFolderAsync(); 
 …
 }  


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.