FileOpenPicker在使用C++的WinUI3中返回内存错误

7z5jn7bk  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(165)

关于我之前的问题-How to get Main Window Handle on Page in WinUI 3 Using C++
我正在使用C++探索WinUI 3,并努力在不同的社区门户网站上找到信息和材料。
我开发了一个演示应用程序,其中有一个窗口和2页。在其中一页,我想打开一个文件选择器。

选项卡1页面. xaml.cpp

void winrt::App1::implementation::Tab1Page::Button_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    OutputTextBlock().Text(OutputTextBlock().Text() + L"Button Clicked\n");

    auto hwnd = GetProcessFirstWindowHandle();

    auto picker = winrt::Windows::Storage::Pickers::FileOpenPicker();
    //Initialize the folder picker with the window handle(HWND).
    auto initializeWithWindow{ picker.as<::IInitializeWithWindow>() };
    initializeWithWindow->Initialize(hwnd);
    picker.SuggestedStartLocation(winrt::Windows::Storage::Pickers::PickerLocationId::Desktop);
    winrt::Windows::Storage::StorageFile file = picker.PickSingleFileAsync().get();     
}

错误

winrt::Windows::Storage::StorageFile file = picker.PickSingleFileAsync().get();
在App1.exe中的0x 00007 FF 9A 92706 BC处抛出异常:Microsoft C++异常:存储器位置0x0000007EA 60 F9 B88出现winrt::hresult_error。

pftdvrlh

pftdvrlh1#

你有两个问题

所以这应该行得通:

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();
}

相关问题