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.