Home > .NET, C#, Windows Forms, WPF > Developing Desktop applications with .NET 5.0

Developing Desktop applications with .NET 5.0

04/01/2021

We know that .NET 5.0 makes it possible to develop WPF and Windows Forms applications. However, at the time of writing, Visual Studio templates haven’t been updated to new target framework: desktop applications are still created using .NET Core 3.1. So, for example, the project file for a WPF application is the following:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

However, we can easily update it to use .NET 5.0. We just need to change Sdk attribute (line 1) and the TargetFramework tag (line 5):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

In particular, we don’t have anymore an Sdk that relates to desktop applications. Instead, we have a target framework moniker (TFM) that is OS-specific, net5.0-windows. This is because the standard net5.0 moniker only includes technologies that work cross-platform. Specifying an OS-specific TFM makes APIs that are specific to an operating system available to ours applications (in our case, desktop applications on Windows). Of course, OS-specific TFMs also inherit every API available to the net5.0 TFM.

After saving this project file and press F5, we can easily verify that our application now run on top of .NET 5.0, as we can see in Output Window of Visual Studio:

A WPF application running on .NET 5.0

A WPF application running on .NET 5.0

So, we have access to all features of .NET 5.0, while still being able to do everything we are accustomed to when developing Windows desktop applications. But it doesn’t ends there: with .NET 5.0 on Windows, we can use C#/WinRT, that provides Windows Runtime (WinRT) projection support for C# language. It allows an application to call Windows 10 APIs through a particular Target Framework Monitor that specifies the version of the Windows 10 SDK to use. At this moment, v17763 (1809), v18362 (1903) and v19041 (2004) are supported. So, for example, if we want to use the latest SDK version, we need the following line:

<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>

Keep in mind that that the SDK version in the TFM is the Windows SDK version used to build our application, not the minimum OS version that it will run on.

In this way, we can use Windows 10 APIs like Geolocation from within a WPF application:

private async void GetLocationButton_Click(object sender, RoutedEventArgs e)
{
    // Geolocator object is part of Windows 10 SDK.
    var locator = new Windows.Devices.Geolocation.Geolocator();
    var location = await locator.GetGeopositionAsync();
    var position = location.Coordinate.Point.Position;

    MessageBox.Show($"Latitude: {position.Latitude:N6}, " +
        $"Longitude: {position.Longitude:N6}");
}

You can find a complete example, for both WPF and Windows Forms, on GitHub.

Categories: .NET, C#, Windows Forms, WPF
  1. 05/01/2021 at 17:04

    Reblogged this on Elliot Balynn's Blog.

  2. Vojo
    17/02/2021 at 10:13

    Great tutorial. Do you mind show how to inject EF Core properly. Thanks

    • 17/02/2021 at 10:19

      Why not, it is an extremely interesting argument, so I’ll take it into considerations, thanks for the suggestion!

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: