TL;DR: Streamline your WPF app’s migration to .NET Core with this 9-step guide! Follow along to set up your environment, ensure API compatibility, manage project files, incorporate NuGet packages, and handle framework-specific code for a seamless transition.
As the world moves toward cross-platform application development, most developers are choosing these methods to build their applications and are migrating their existing applications to platforms that make this easier. In this blog, I am going to walk you through porting an existing WPF application to .NET Core in 9 easy steps.
For my example, I am choosing ExpenseAnalysis, a Syncfusion WPF showcase application, to port to a .NET Core application.
To develop a .NET Core WPF application, you need Visual Studio 2019 and .NET Core 3.0 installed in your machine.
Before starting to port an application, check whether all the APIs are compatible using .NET Portability Analyzer.
Now add a new .csproj file. In this example, ExpenseAnalysisDemo_NetCore.csproj is added to the same folder where the other .NET .csproj files are placed. Add the following content in to the new .csproj file.
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>netcoreapp3.0</TargetFramework> <UseWPF>true</UseWPF> </PropertyGroup> </Project>
Specify the assembly name and default namespace in the new project file as it is in the .NET project.
<AssemblyName>ExpenseAnalysisDemo</AssemblyName> <RootNamespace>ExpenseAnalysisDemo</RootNamespace>
A .NET Core project will automatically generate an AssemblyInfo.cs file. As we already have it, we need to suppress the auto generation by setting GenerateAssemblyInfo to false.
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
You don’t have to add any .cs or .xaml. They will automatically be added.
Unlike in .NET projects, the packages.config file will not be considered in .NET Core. Instead, you have to add the necessary NuGet package reference in the .NET Core project file.
<ItemGroup> <PackageReference Include="Syncfusion.Compression.Base" version="17.2.0.35" /> <PackageReference Include="Syncfusion.Data.WPF" version="17.2.0.35" /> <PackageReference Include="Syncfusion.DataGridExcelExport.Wpf" version="17.2.0.35"/> <PackageReference Include="Syncfusion.Licensing" version="17.2.0.35" /> <PackageReference Include="Syncfusion.Pdf.Wpf" version="17.2.0.35" /> <PackageReference Include="Syncfusion.SfChart.WPF" version="17.2.0.35" /> <PackageReference Include="Syncfusion.SfGrid.WPF" version="17.2.0.35" /> <PackageReference Include="Syncfusion.Shared.WPF" version="17.2.0.35" /> <PackageReference Include="Syncfusion.XlsIO.Wpf" version="17.2.0.35" /> <PackageReference Include="Syncfusion.XlsIO.Wpf" version="17.2.0.35" /> </ItemGroup>
The project may use different resources and images for various purposes. Add these resources to the .NET Core project manually in the ItemGroup section.
<Resource Include="**\*.png" /> <Resource Include="App.ico" />
Open the project file in Visual Studio 2019 and run the Expense Analysis showcase.
You can now interact with this report and use grids, charts with filtering, pagination, exporting, etc.
After exporting data in the DataGrid, Expense Analysis will suggest opening the file. On choosing yes, the file will not open, as Process.Start() will lead to a Win32Exception: “The specified executable is not a valid application for this OS platform.”
Opening a file through .NET Core is different from .NET Framework, so use condition compilation to resolve this issue.
#if !NETCORE System.Diagnostics.Process.Start(sfd.FileName); #else ProcessStartInfo psi = new ProcessStartInfo { FileName = "cmd", WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, CreateNoWindow = true, Arguments = "/c start " + sfd.FileName }; Process.Start(psi); #endif
With this, we have successfully migrated a WPF .NET Framework project to .NET Core. The migrated application of this converted project is provided for your reference in this GitHub demos.
You can always contact us using Direct Trac, our support forum, or the feedback portal. We are happy to assist you.