How to open a WinUI Window
from a WinForms project
#4203
-
I am trying to open a WinUI Update: But as soon as I call
There is no message. I have installed the latest WindowsAppSDK (1.4.240211001) via NuGet in both the WinForms project and the WinUI class library project and added I was trying to get it working with this code but, as mentioned, it fails on the first line:
Thanks for any pointers. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Have you initialised XAML properly on the thread you are trying to create the Window instance on? At the very least you need a class that derives from Microsoft.UI.Xaml.Application which also implements Microsoft.UI.Xaml.Markup.IXamlMetadataProvider. You would also need a Microsoft.UI.Dispatching.DispatcherQueue active on the thread that you create all of this on. |
Beta Was this translation helpful? Give feedback.
-
I managed to get much further thanks to @castorix 's CSharp_WinUI3_DesktopWindowXamlSource example! I replaced my Then from my MainForm I call this method (code got from Form1.cs): private Microsoft.UI.Xaml.Hosting.DesktopWindowXamlSource dwxs = null;
public Microsoft.UI.Windowing.AppWindow MainFormAppWindow { get; set; }
private Microsoft.UI.Xaml.Window window;
public void OpenWinUIWindow(MainForm mainForm)
{
if (MainFormAppWindow == null)
{
IntPtr hwnd = mainForm.Handle;
WindowId mainFormWindowId = Win32Interop.GetWindowIdFromWindow(hwnd);
MainFormAppWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(mainFormWindowId);
}
if (dwxs == null)
{
// https://github.com/castorix/CSharp_WinUI3_DesktopWindowXamlSource/blob/master/Form1.cs
Microsoft.UI.Xaml.Hosting.WindowsXamlManager.InitializeForCurrentThread();
dwxs = new Microsoft.UI.Xaml.Hosting.DesktopWindowXamlSource();
dwxs.Initialize(MainFormAppWindow.Id);
}
if (window == null)
{
var window = new MyWinUIWindow();
window.Title = "MyWinUI";
window.AppWindow.ResizeClient(new SizeInt32(600, 400));
window.Activate();
};
} And now the WinUI window opens. However there are still some issues:
I'd be grateful for any pointers to get further. |
Beta Was this translation helpful? Give feedback.
-
Hi @karmeye, I've just created a feature request that may interest you. I've aimed to define the scope of the use case as broadly as possible. |
Beta Was this translation helpful? Give feedback.
Well I wrote a quick and dirty C++ example of how to separate things.
wuiinterop.zip
While it is C++ and doesn't use Windows forms, the concept itself works.
There are a couple of notes on this.
First, the WinUI 3 Xaml compiler is enabled. This mean the automatic generation of App and the Window runtime classes is enabled. This is easy to do in the case of the Windows API and most likely WinForms. If you want to do this with WPF then you would likely also need to separate out the WPF or WinUI 3 into a separate library.
Second, the event loops for WinUI 3 and the Windows API are completely independent. The biggest issue this causes is that you must create the windows on the appropriate thr…