Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a WPF app with .NET Core 3 and Visual Studio

What is or will be the correct way of creating WPF applications with .NET Core 3 and WPF Desktop Pack with Visual Studio 2017?

Will there be a new project template in the New Project window or will this be done through NuGet or some other way?

like image 585
Dean Kuga Avatar asked Sep 21 '18 16:09

Dean Kuga


People also ask

Can I use .NET core for WPF?

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

Is WPF still relevant 2022?

“WPF would be dead in 2022 because Microsoft doesn't need to be promoting non-mobile and non-cloud technology. But WPF might be alive in that sense if it's the best solution for fulfilling specific customer needs today. Therefore, having a hefty desktop application needs to run on Windows 7 PCs with IE 8.


1 Answers

Brian Lagunas recently posted Getting Started with .NET Core 3 – Create a WPF Application video on his website which provides detailed instructions on how to create a new WPF application targeting .NET Core 3.

Here is the short of it:

  • Install .NET Core 3 and SDK:

https://github.com/dotnet/core-setup

https://github.com/dotnet/core-sdk

  • Create new WPF Application targeting .NET Framework.

  • Unload project.

  • Edit csproj file by removing everything and replacing it with the following:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>netcoreapp3.0</TargetFramework>
  <UseWPF>true</UseWPF>
  <AssemblyName>MyCore3App</AssemblyName>
  </PropertyGroup>  
</Project>
  • Reload project.

  • Build project.

  • If the build fails and your Output window is mentioning duplicate attributes delete the Properties folder.

Watch the video for additional valuable information and keep in mind that .net Core 3 is still in development and these instructions could become inaccurate at any time. I will update the answer as I become aware of any changes impacting this process.

Update 2018-12-05:

.NET Core 3 Preview 1 was released yesterday, here are the important bits from the announcement:

  • Visual Studio 2019 will be the release to support building .NET Core 3 applications.

  • Windows Desktop Frameworks are now Open Source and the first wave of source code is already available on GitHub.

  • You can create new .NET Core projects for WPF and Windows Forms from the command line.

dotnet new wpf

  • You can also open, launch and debug WPF and Windows Forms projects in Visual Studio 2019 Preview 1. It is currently possible to open .NET Core 3.0 projects in Visual Studio 2017 15.9, however, it is not a supported scenario (and you need to enable previews).

  • csproj file format shown above is still valid.

  • dotnet build now copies NuGet dependencies for your application from the NuGet cache to your build output folder during the build operation. Until this release,those dependencies were only copied as part of dotnet publish. This change allows you to xcopy your build output to different machines.

Update 2019-03-15:

Recently released Visual Studio 2019 RC has a project template for both WPF and Windows Forms targeting .NET Core platform making it very easy to create new WPF and Windows Forms .Net Core applications.

Update 2019-11-04:

.NET Core 3.0 was released on 2019-09-23.

WPF is now fully supported and .NET Core WPF App project template is available within the "Create a new project" dialog.

Project properties of special interest are PublishReadyToRun, PublishSingleFile, and RuntimeIdentifier.

This is what a typicall PropertyGroup section would look like with all three options:

<PropertyGroup>
  <OutputType>WinExe</OutputType>
  <TargetFramework>netcoreapp3.0</TargetFramework>
  <UseWPF>true</UseWPF>
  <PublishReadyToRun>true</PublishReadyToRun>
  <PublishSingleFile>true</PublishSingleFile>
  <RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>

These options will allow you to make your WPF Core application a completely self-contained, 64-bit, fast startup executable that will run on any 64-bit version of Windows (just omit the win-x64 runtime identifier if you need it to run on Windows x86) from any location including USB drives.

The most fantastic thing about WPF Core for all of us Windows desktop developers is that this completely eliminates all the "deployment blues" of the past. You no longer have to worry about what OS the end user is running, what version of .NET framework is available on the target machine or if the user has sufficient permissions to install your app. You can distribute a single file that will run from %LOCALAPPDATA% folder or any location user chooses and it will all just work.

Because the resulting executable will contain the .NET Core itself and all the referenced libraries, this will result in a rather large file but it is a small price to pay for all the benefits you get from creating a single self-contained executable. When published like this, my most recent app was 175 MB which I don't consider large at all in 2020.

However, if you do find the file too large for your liking, you can experiment with a tool called Warp and PublishTrimmed property which is supposed to reduce the file size by analyzing your code and only including portions of the framework used in your application. This could lead to excluding some required pieces, however, resulting in a non-working executable, especially if your code is using reflection. There is a great blog post by Scott Hanselman out there going over all of this in more detail.

like image 153
Dean Kuga Avatar answered Sep 27 '22 21:09

Dean Kuga