How to get the HWND handle of elements in .Net MAUI
See the question and my original answer on StackOverflowWinUI3 is the technology used by MAUI on Windows, and you can refer to Windowing overview for WinUI and Windows App SDK to understand the link between a WinUI3 Window to a Win32 HWND.
Then, you can use Platform lifecycle events to get it, something like this:
using Microsoft.Maui.LifecycleEvents;
namespace MauiApp1
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureLifecycleEvents(events =>
{
#if WINDOWS
events.AddWindows(bld =>
{
// window here is of Microsoft.UI.Xaml.Window type (WinUI3)
bld.OnWindowCreated(window =>
{
var hwnd = Microsoft.UI.Win32Interop.GetWindowFromWindowId(window.AppWindow.Id);
});
});
#endif
});
return builder.Build();
}
}
}