Trouble in using FileOpenPicker

Hong 1,246 Reputation points
2023-12-18T01:59:41.4033333+00:00

"Invalid window handle. (0x80070578)" exception will through if FileOpenPicker is used the same way as for UWP. A remedy needs to be used.

FileOpenPicker picker = new FileOpenPicker()
{
    SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
    FileTypeFilter = { ".ofx" }
};
picker.ViewMode = PickerViewMode.List;
nint windowHandle = WindowNative.GetWindowHandle((Application.Current as App)?.MainWindow);
InitializeWithWindow.Initialize(picker, windowHandle);
StorageFile file = await picker.PickSingleFileAsync();


await picker.PickSingleFileAsync() throws a different exception:

User's image

I have no clue about what this exception means. Could anyone shed some light on this?

Windows development Windows App SDK
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-12-18T06:44:08.6566667+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I've already read the reference link you shared. I understand that you are trying to use FileOpenPicker in a Page Control, is that right?

    When running you code, the issue happens when using (Application.Current as App)?.MainWindow as the parameter for WindowNative.GetWindowHandle() method. Using Application.Current as App will not get the app object that contains the MainWindow. I'd suggest use the way which mentioned by the Andrew KeepCoding from the link you shared. Just call App.Window to get the window you need.

    For example:

      public static Window? Window { get; private set; }
      /// <summary>
      /// Invoked when the application is launched.
      /// </summary>
      /// <param name="args">Details about the launch request and process.</param>
      protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
      {
          m_window = new MainWindow();
    
          Window = m_window;
    
          m_window.Activate();
          
      }
    
    
      FileOpenPicker picker = new FileOpenPicker()
      {
          SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
          FileTypeFilter = { ".ofx" }
      };
      picker.ViewMode = PickerViewMode.List;
      nint windowHandle = WindowNative.GetWindowHandle(App.Window);
      InitializeWithWindow.Initialize(picker, windowHandle);
      StorageFile file = await picker.PickSingleFileAsync();
    

    Using the above code, I don't get any exception when trying to open a file picker in a Page.

    'Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.