ChuckBohling-9818 avatar image
2 Votes"
ChuckBohling-9818 asked ChuckBohling-9818 commented

Using UWP's FileOpenPicker in a WPF app fails.

I'm trying to use UWP's FileOpenPicker with a WPF app but it's not working. It sounds like it should. At first, when I called FileOpenPicker.PickMultipleFilesAsync, it failed with an "invalid window handle". I found some code online that fixes that problem but there are still issues. Here's the code:

 var openPicker = new FileOpenPicker()
  ViewMode = PickerViewMode.Thumbnail,
  SuggestedStartLocation = PickerLocationId.Desktop
 var result = await openPicker.PickMultipleFilesAsync();
 var count = result.Count;

With the call to IInitializeWithWindow.Initialize, PickMultipleFilesAsync works. However, now the call to result.Count fails.

Here's the exception:

   Message=Unable to cast COM object of type 'Windows.Storage.Pickers.FilePickerSelectedFilesArray' to interface type 'System.Runtime.InteropServices.WindowsRuntime.IVectorView`1[Windows.Storage.StorageFile]'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{80646519-5E2A-595D-A8CD-2A24B4067F1B}' failed due to the following error: The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD)).
    at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRTSharedGeneric(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)
    at System.Runtime.InteropServices.WindowsRuntime.IVectorView`1.get_Size()
    at System.Runtime.InteropServices.WindowsRuntime.VectorViewToReadOnlyCollectionAdapter.Count[T]()
    at WpfApp1.MainWindow.<Button_Click>d__2.MoveNext() in c:\test\WpfApp1\MainWindow.xaml.cs:line 39

Any idea what's wrong and how to fix the problem?

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, FileOpenPicker is a UWP function, not applicable to win32 applications. In WPF, please use OpenFileDialog

1 Vote 1 ·

Thanks. What a really want is the WPF equivalent of UWP's FolderPicker which apparently doesn't exist. From some of the online conversations, doing IInitializeWithWindow.Initialize would allow FolderPicker to work. Other people were having success. Has something changed?

0 Votes 0 ·

Hello, the folder selected in WPF has FolderBrowserDialog, which has a similar effect as FolderPicker, can it not meet your needs?

0 Votes 0 ·

I don't believe that FolderBrowserDialog is supported in .Net Core 3.1 which I'm using. Good news though. I found a nuget package, WindowsAPICodePack, that has the functionality that I need.

0 Votes 0 ·

Hi, glad you found a solution, you can try to summarize it as an answer and accept it, this will help more people who have encountered the same problem.

0 Votes 0 ·

1 Answer

ChuckBohling-9818 avatar image
0 Votes"
ChuckBohling-9818 answered

To get the same functionality as UWP's FileOpenPicker and FolderPicker in a .net core 3.1 WPF app, you can use the nuget package WindowsAPICodePack. All of these appear to be wrappers around the Win32 Common Item Dialog (IFileDialog).

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.