App Dev Manager Nayan Patel explores how to modernize desktop applications using Xaml Islands.
Consider a customer that has a Win32 desktop app written in C++ (MFC). A major issue has been that in order to modernize their UI, they would have to create an entirely new app using WPF or UWP. This is exactly why the WinUI team created Xaml Islands so they can bridge this gap. Xaml Islands enables developers to enhance the look, feel and functionality of legacy Win32 C++ or WPF apps using UWP controls without having to do a complete rewrite. Xaml islands also offers them the ability to leverage new UI controls on specific parts of the app so they can modernize at their own pace.
Starting from Windows 10 v1903 (or later), the UWP XAML hosting API is available, as part of Win10 SDK, and can be used in legacy desktop applications. This API enables the association of UWP Xaml controls with window handles (HWND) and allows developers to use UWP controls inside of native C++ Win32 applications. XAML Islands can help leverage new controls and new API’s from your legacy code and runs on the same UI thread of your desktop app. You can access all the UWP XAML objects from your code behind without doing any marshalling.
Below diagram shows how the different types of XAML Island controls are organized architecturally on top of the UWP XAML hosting API. The host controls and wrapped controls are available via NuGet packages in the Windows Community Toolkit.
In order to use XAML islands inside an existing C++ Win32 app, follow these basic steps
- Initialize the UWP XAML framework in the current thread using the static InitializeForCurrentThread of the WindowsXamlManager class
- Create a DesktopWindowXamlSource object that requires the HWND of your app. The DesktopWindowXamlSource will create a ChildWindow where you can place the XAML content.
- Finally, assign the Windows.UI.Xaml.UIElement you want to host to the Content property of your DesktopWindowXamlSource object
For complete examples, check out the following sample projects that show XAML islands used in C++ Win32 apps.
0 comments