9 Easy Steps to Port a WPF Application to .NET Core

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.

Step 1: Set up the environment.

To develop a .NET Core WPF application, you need Visual Studio 2019 and .NET Core 3.0 installed in your machine.

Step 2: Check that all the APIs are .NET Core compatible.

Before starting to port an application, check whether all the APIs are compatible using .NET Portability Analyzer.

Step 3: Create the project file.

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>

Step 4: Declare assembly name and default namespace.

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>

Step 5: Suppress auto generation of AssemblyInfo.

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.

Step 6: Add necessary NuGet packages in the project file.

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>

Step 7: Add necessary resources to the project file.

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" />

Step 8: Open and run.

Open the project file in Visual Studio 2019 and run the Expense Analysis showcase.Expense Analysis .NET Core project output

You can now interact with this report and use grids, charts with filtering, pagination, exporting, etc.

Step 9: Use condition compilation for framework-specific code.

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.”View Confirmation

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

Conclusion

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 location.

All Syncfusion WPF and WinForms components support .NET Core 3.0. From the recent 17.2 release, you can run existing WPF and WinForms product showcase demos in both .NET Core and .NET Framework.WPF Dashboard

Download the Syncfusion WinForms and WPF samples from their respective locations and use these controls for your development needs in .NET Core. Share your feedback in the comment section.

You can always contact us using Direct Trac, our support forum, or the feedback portal. We are happy to assist you.

Tags:

Share this post:

Related Posts

Comments (1)

[…] on August 20, 2019by admin submitted by /u/prabakarinfo [link] [comments] No comments […]

Leave a comment