Using UWP's FileOpenPicker in a WPF app fails.

Chuck Bohling 346 Reputation points

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?

Universal Windows Platform (UWP)
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
{count} votes

Accepted answer
  1. Chuck Bohling 346 Reputation points

    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).

    0 comments No comments

0 additional answers

Sort by: Most helpful