我是新来的UWP/WinRT!
我有这样的代码:
void MainPage::processButton_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
winrt::Windows::Storage::Pickers::FileOpenPicker picker;
picker.ViewMode(winrt::Windows::Storage::Pickers::PickerViewMode::Thumbnail);
picker.FileTypeFilter().Append(L".mp4");
auto filename= picker.PickSingleFileAsync().GetResults();
}
当我运行这段代码时,我在运行时得到了这个错误:
在__debugbreak中执行断点指令(__debugbreak()语句或类似的调用)。
将代码更改为此代码:
void MainPage::processButton_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
winrt::Windows::Storage::Pickers::FileOpenPicker picker;
picker.ViewMode(winrt::Windows::Storage::Pickers::PickerViewMode::Thumbnail);
picker.FileTypeFilter().Append(L".mp4");
auto filename= picker.PickSingleFileAsync().GetResults();
}
产生同样的错误。
如何等待选择器完成它的工作并返回我可以在C++ UWp/WinRT中打开的文件?
发布于 2022-10-02 19:06:45
当您试图在尚未运行到完成的GetResults
上调用IAsyncOperation
时,会引发您得到的诊断。
为了解决这个问题,您需要异步地等待操作完成。最直接的方法是使用协同线。使用C++/WinRT的并发和异步操作拥有您所需的所有信息。
通过将代码更改为以下内容,可以将其应用于代码:
IAsyncAction MainPage::processButton_Click(IInspectable sender, RoutedEventArgs e)
{
winrt::Windows::Storage::Pickers::FileOpenPicker picker;
picker.ViewMode(winrt::Windows::Storage::Pickers::PickerViewMode::Thumbnail);
picker.FileTypeFilter().Append(L".mp4");
auto filename = co_await picker.PickSingleFileAsync();
}
确保将签名从引用更改为按值接收参数。如果你不这样做,事情就会以完全难以诊断的方式破裂。
https://stackoverflow.com/questions/73926853
复制相似问题