In 2018, Microsoft announced some exciting news about the Windows Presentation Foundation (WPF) and Windows Forms (WinForms): they are now compatible with .NET Core. This update breathes new life into these technologies, which are the main ones used to create Windows applications, including Windows 10. In this article, I will discuss how to use WPF and WinForms with .NET Core 3.0, talk about a useful tool for migrating WPF and WinForms projects to .NET Core, and explain how to reuse Universal Windows Platform (UWP) controls in these kinds of projects with XAML Islands.
WPF and WinForms
Many developers like WPF and WinForms because they make it very easy to create interfaces and interact with Windows. Now, with .NET Core, we are able to migrate old projects to the latest .NET Core version.
.NET Core, as an efficient, completely open-source framework, has an important role in these kinds of projects. Using the .NET Standard library, we can reuse the code in other kinds of projects with technologies such as ASP.NET Core and Xamarin.
In order to use WPF and WinForms with .NET Core, we need to install version 3.0 of .NET Core in our environments. You can find the installer here.
Once .NET Core 3.0 is installed, you can use “dotnet new wpf” to create a new WPF project.
Using the command “dir”, we get the files created, and, as usual, we execute the application with “dotnet run”.
Ninety-nine percent of the features and tools that are available in WPF for the .NET framework are also available in this new version of .NET Core, and we can use Visual Studio to design our interfaces just as easily as before.
Even though in .NET Core the dependency on Visual Studio was removed, it is still useful for designing our interfaces. The project is available in Visual Studio after installing .NET Core 3.0 .
At this moment, many WPF applications are in production and must be migrated to .NET Core. Normally, this is hard manual work involving a lot of research to provide proper support for the migrated applications without breaking anything.
Microsoft released an open-source tool allowing us to easily analyze the portability of our applications and associated libraries. With this portability analyzer, we can get a useful report including all the libraries that need to be updated or are not compatible.
This is an example of the report generated by the portability analyzer. You can check out this article for more information.
Using XAML Islands with WPF and WinForms
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.