Hi,
I am having trouble making a code sample to work in C++/WinRT based on a C# sample and it seems to me if I could understand how to "translate" C# code to C++/WinRT I would be home free.
The original C# sample is as follows:
FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.FileTypeFilter.Add(".jpg"); StorageFile file = await openPicker.PickSingleFileAsync();
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<StorageFile> 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 Dent