Search code examples
c#uwp

Calling an asynchronous method causes a debug


I ran into a problem while developing a program on UWP. I need to open a dialog box for the user to select a save folder. When calling the method StorageFile file = await openPicker.PickSingleFileAsync(); the program calls the debugger.

The complete method:

private async void OpenFolderButton_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

    StorageFile file = await openPicker.PickSingleFileAsync();
    if (file != null)
        filePath.Text = file.Path;
}

I tried to look at the code examples on the Microsoft website.


Solution

  • You should add FileTypeFilter to specify file types.

           FileOpenPicker openPicker = new FileOpenPicker();
           openPicker.ViewMode = PickerViewMode.Thumbnail;
           openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
           // set file type to show
           openPicker.FileTypeFilter.Add("*");
           StorageFile file = await openPicker.PickSingleFileAsync();
    

    For more details you could refer to the Doc:Pick a single file: step-by-step