I have been able to come up with the following C++/winRT code which "almost works". I created a class with a method that returns IAsyncOperation so that I could use the co_await keyword; the method looks like this:
IAsyncOperation<StorageFile> FileDialogs::chooseFileToOpen() const
{
FileOpenPicker picker{};
picker.ViewMode(PickerViewMode::Thumbnail);
picker.SuggestedStartLocation(PickerLocationId::DocumentsLibrary);
picker.FileTypeFilter().Append(L".jpg");
picker.FileTypeFilter().Append(L".png");
StorageFile file = co_await picker.PickSingleFileAsync();
co_return file;
}
Now I call this method from the click handler of a button:
void MainPage::btnUpdateSource_Click(IInspectable const & sender,
RoutedEventArgs const & args)
{
FileDialogs fd{};
auto res = fd.chooseFileToOpen();
StorageFile file = res.get();
auto name = file.Name();
///.....
}
This compiles but when the get() method is called it fires this assert:
WINRT_ASSERT(!is_sta());
So it is complaining that the apartment is single-threaded! and yet winrt::init_apartment() was called with multithreading!!
So that's where I am stuck!
Can anyone give me some insight? I am using Visual Studio 2017 version 15.7.5.
Thanks, Juan