Quantcast
Channel: Parallel Computing in C++ and Native Code forum
Viewing all articles
Browse latest Browse all 856

How can I get the chosen file from a FileOpenPicker window in C++ winRT?

$
0
0

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


Viewing all articles
Browse latest Browse all 856

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>