Hesam Seyed Mousavi, June 30, 2020
XAML Islands is the best way to reuse UWP UI controls with WPF and WinForms. With this approach, you can potentiate your UI interfaces and easily implement useful controls that in other cases would need to be built manually.
The following nuggets are available to be included in the project and contain the controls that we can reuse:
• Microsoft.Toolkit.Wpf.UI.XamlHost – This package provides the WindowsXamlHost class for WPF.
• Microsoft.Toolkit.Wpf.UI.Controls – As of right now, this package provides wrapper classes for first party controls, such as the InkCanvas, InkToolbar, MapControl, and MediaPlayerElement, all for WPF.
• Microsoft.Toolkit.Forms.UI.XamlHost – This package provides the WindowsXamlHost class for Windows Forms.
• Microsoft.Toolkit.Forms.UI.Controls – As of right now, this package provides wrapper classes for first party controls, such as the InkCanvas, InkToolbar, MapControl, and MediaPlayerElement, all for Windows Forms.
These nuggets, or packages, are currently (as of September 2019) in preview status, but you can still add them to your project by using the nugget interfaces or just adding the reference to them in the .csproj file:
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.Controls" Version="6.0.0-preview5" />
In this new version of WPF with .NET Core, we can open the “.csproj” file by just double clicking on the solution explorer. This file still has an XML format.
In the following example, we have the map control inside the WPF view:
The code is finished, but it is still not going to work properly at this point. You either need to include the manifest of the application (UWP needs this file for application deployment) or create a Windows application packaging project in order to include UWP compatibility.
Set a name for the project and create it.
Currently, you need to include the Insider preview in order to include UWP control. To get the Insider version of Windows, follow this link.
Currently, the Insider version is the only one that is supporting these new features. For more information, check out the Windows Insider program blog.
Finally, after running the application, we get the UI with the map control. This is pretty amazing, bearing in mind that map control only works in UWP.
In conclusion, WPF and WinForms are powerful tools that you can use to create amazing desktop applications for Windows 10. With .NET Core, you have a high-performing, open-source platform and are able to use the .NET Standard library as a great way to reuse the code in other kinds of projects with technologies like ASP.NET Core and Xamarin.
Additionally, XAML Islands provides an easy way to use UWP controls, allowing you to avoid any direct UWP dependencies and use the platform that best fits your needs. UWP is not obsolete—this platform is the only way to create generic UIs for different technologies ranging from Xbox to the IoT. The decision to build a new application in UWP, WPF, or WinForms ultimately depends on the scope, context, and requirements.