Fixing FileOpenPicker memory error in WinUI 3
See the question and my original answer on StackOverflowYou have two problems
- You must have at least one item in the FileTypeFilters collection as you saw in debug output
- Your code must run on the UI thread
so this should work:
Windows::Foundation::IAsyncAction winrt::App1::implementation::Tab1Page::Button_Click(IInspectable const& sender, RoutedEventArgs const& args)
{
auto hwnd = GetFirstProcessWindowHandle();
auto picker = winrt::Windows::Storage::Pickers::FileOpenPicker();
picker.FileTypeFilter().Append(L"*");
auto initializeWithWindow{ picker.as<IInitializeWithWindow>() };
initializeWithWindow->Initialize(hwnd);
picker.SuggestedStartLocation(winrt::Windows::Storage::Pickers::PickerLocationId::Desktop);
auto file = co_await picker.PickSingleFileAsync();
}