Display WinRT UI objects that depend on CoreWindow
Article
Certain pickers, popups, dialogs, and other Windows Runtime (WinRT) objects depend on a CoreWindow; typically to display a user-interface (UI). Even though CoreWindow isn't supported in desktop apps (see Core unsupported classes), you can still use many of those WinRT classes in your desktop app by adding a little bit of interoperation code.
Set the owner window handle (HWND) for a WinRT UI object
For classes that implement the IInitializeWithWindow interface (or the equivalent IDataTransferManagerInterop interface), you can use that interface to set an owner window on the object before you display it. It's a two-step process.
Decide which window will be the owner of the UI object that you want to display, and retrieve that window's HWND. For more details and code examples for this step, see Retrieve a window handle (HWND).
Then call the appropriate interoperatability API (for C# or C++/WinRT) to set an owner window handle (HWND) for the WinRT UI object.
The list above is necessarily incomplete—refer to a type's documentation to see whether it implements IInitializeWithWindow (or an equivalent interop interface).
The next sections contain code examples to display a FolderPicker. But it's the same technique to display any of the APIs listed above.
WinUI 3 with C# (also WPF/WinForms with .NET 6 or later)
Note
The code examples in this section use the WinRT.Interop.WindowNative C# interop class. If you target .NET 6 or later, then you can use that class in a WPF or WinForms project. For info about setting up your project to do that, see Call interop APIs from a .NET app.
The C# code below expects that you've already used the pattern documented in Retrieve a window handle (HWND). Then, to set the owner window for the UI object that you want to display, the code calls the Initialize method on the WinRT.Interop.InitializeWithWindow C# interop class. For more info about the C# interop classes, see Call interop APIs from a .NET app.
C#
// MainWindow.xaml.csprivateasyncvoidShowFolderPickerAsync(IntPtr hWnd)
{
// Create a folder picker.var folderPicker = new Windows.Storage.Pickers.FolderPicker();
// Initialize the folder picker with the window handle (HWND).
WinRT.Interop.InitializeWithWindow.Initialize(folderPicker, hWnd);
// Use the folder picker as usual.
folderPicker.FileTypeFilter.Add("*");
var folder = await folderPicker.PickSingleFolderAsync();
}
WinUI 3 with C++
The C++/WinRT code below expects that you've already used the pattern documented in Retrieve a window handle (HWND). Then, to set the owner window for the UI object that you want to display, the code calls the interoperatability method IInitializeWithWindow::Initialize.
C++/WinRT
// pch.h
...
#include<microsoft.ui.xaml.window.h>#include<Shobjidl.h>#include<winrt/Windows.Storage.Pickers.h>// MainWindow.xaml.cpp
winrt::fire_and_forget ShowFolderPickerAsync(HWND hWnd){
// Create a folder picker.
Windows::Storage::Pickers::FolderPicker folderPicker;
// Initialize the folder picker with the window handle (HWND).auto initializeWithWindow{ folderPicker.as<::IInitializeWithWindow>() };
initializeWithWindow->Initialize(hWnd);
// Use the folder picker as usual.
folderPicker.FileTypeFilter().Append(L"*");
auto folder{ co_await folderPicker.PickSingleFolderAsync() };
}
For classes that implement IDataTransferManagerInterop
C#. Call the RequestVerificationForWindowAsync method of the Windows.Security.Credentials.UI.UserConsentVerifierInterop C# interop class. For more info about the C# interop classes, see Call interop APIs from a .NET app.
For classes that implement other interop interfaces
These interfaces have XxxForWindow methods, which let you set an owner window handle (HWND). You can use these interfaces directly from C++/WinRT. Versions of the interfaces also exist in the form of C# classes—for more details, see Call interop APIs from a .NET app.
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Windows developer feedback
Windows developer is an open source project. Select a link to provide feedback:
Windows developers have various options for creating applications that run on Windows. This module introduces the native Windows UI frameworks that are available for Windows development. It also provides guidance on how to choose the best framework for your application.