Upgrade a WPF app to .NET 7 - WPF .NET (2023)

  • Article

This article describes how to upgrade a Windows Presentation Foundation (WPF) desktop app to .NET 7. Even though WPF runs on .NET, a cross-platform technology, WPF is still a Windows-only framework. The following WPF-related project types can be upgraded with the .NET Upgrade Assistant:

  • WPF project
  • Control library
  • .NET library

If you're upgrading from .NET Framework to .NET, consider reviewing the Differences with WPF .NET article and the Porting from .NET Framework to .NET guide.

Warning

Don't upgrade Visual Basic WPF projects. There seems to be a bug with the extension. This article will be updated when the bug is fixed.

Prerequisites

  • Windows Operating System
  • Visual Studio 2022 version 17.1 or later to target .NET 7
  • Visual Studio 2022 version 17.7 Preview 1 or later to target .NET 8
  • .NET Upgrade Assistant extension for Visual Studio

Demo app

This article was written in the context of upgrading the Web Favorites Sample project, which you can download from the .NET Samples GitHub repository.

Initiate the upgrade

If you're upgrading multiple projects, start with projects that have no dependencies. In the Web Favorites sample, the WebSiteRatings project depends on the StarVoteControl library, so StarVoteControl should be upgraded first.

Tip

Be sure to have a backup of your code, such as in source control or a copy.

Use the following steps to upgrade a project in Visual Studio:

  1. Right-click on the StarVoteControl project in the Solution Explorer window and select Upgrade:

    Upgrade a WPF app to .NET 7 - WPF .NET (1)

    A new tab is opened that prompts you to choose how you want the upgrade to be performed.

  2. Select In-place project upgrade.

  3. Next, select the target framework. Based on the type of project you're upgrading, different options are presented. .NET Standard 2.0 is a good choice if the library doesn't rely on a desktop technology like WPF and can be used by both .NET Framework projects and .NET projects. However, the latest .NET releases provide many language and compiler improvements over .NET Standard.

    Select .NET 7.0 and then select Next.

    Upgrade a WPF app to .NET 7 - WPF .NET (2)

  4. A tree is shown with all of the artifacts related to the project, such as code files and libraries. You can upgrade individual artifacts or the entire project, which is the default. Select Upgrade selection to start the upgrade.

    When the upgrade is finished, the results are displayed:

    Upgrade a WPF app to .NET 7 - WPF .NET (3)

    Artifacts with a solid green circle were upgraded while empty green circles were skipped. Skipped artifacts mean that the upgrade assistant didn't find anything to upgrade.

Now that the app's supporting library is upgraded, upgrade the main app.

Upgrade the app

Once all of the supporting libraries are upgraded, the main app project can be upgraded. Perform the following steps:

  1. Right-click on the WebSiteRatings project in the Solution Explorer window and select Upgrade:
  2. Select In-place project upgrade as the upgrade mode.
  3. Select .NET 7.0 for the target framework and select Next.
  4. Leave all of the artifacts selected and select Upgrade selection.

After the upgrade is complete, the results are shown. If an item has a warning symbol, it means that there's a note for you to read, which you can do by expanding the item.

Generate a clean build

After your project is upgraded, clean and compile it.

  1. Right-click on the WebSiteRatings project in the Solution Explorer window and select Clean.
  2. Right-click on the WebSiteRatings project in the Solution Explorer window and select Build.

If your application encountered any errors, you can find them in the Error List window with a recommendation how to fix them.

Post-upgrade steps

If your project is being upgraded from .NET Framework to .NET, review the information in the Modernize after upgrading to .NET from .NET Framework article.

After upgrading, you'll want to:

  • Check your NuGet packages.

    The .NET Upgrade Assistant upgraded some packages to new versions. With the sample app provided in this article, the Microsoft.Data.Sqlite NuGet package was upgraded from 1.0.0 to 7.0.5. However, 1.0.0 depends on the SQLite NuGet package, but 7.0.5 removes that dependency. The SQLite NuGet package is still referenced by the project, although it's no longer required. Both SQLite and SQLite.Native NuGet packages can be removed from the project.

  • Clean up the old NuGet packages.

    The packages.config file is no longer required and can be deleted from your project, as the NuGet package references are now declared in the project file. Additionally, the local NuGet package cache folder, named Packages, is in either the folder or the parent folder of the project. This local cache folder can be deleted. The new NuGet package references use a global cache folder for packages, available in the user's profile directory, named .nuget\packages.

  • Remove the System.Configuration library.

    Most .NET Framework apps reference the System.Configuration library. After upgrading, it's possible that this library is still directly referenced.

    The System.Configuration library uses the app.config file to provide run-time configuration options to your app. For .NET, this library was replaced by the System.Configuration.ConfigurationManager NuGet package. Remove reference to the library and add the NuGet package to your project.

  • Check for places to modernize your app.

    APIs and libraries have changed quite a bit since .NET was released. And in most cases, .NET Framework doesn't have access to these improvements. By upgrading to .NET, your now has access to more modern libraries.

    The next sections describe areas you modernize the sample app used by this article.

Modernize: Web browser control

The WebBrowser control referenced by the WPF sample app is based on Internet Explorer, which is out-of-date. WPF for .NET can use the WebView2 control based on Microsoft Edge. Complete the following steps to upgrade to the new WebView2 web browser control:

  1. Add the Microsoft.Web.WebView2 NuGet package.

  2. In the MainWindow.xaml file:

    1. Import the control to the wpfControls namespace in the root element:

      <mah:MetroWindow x:Class="WebSiteRatings.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" xmlns:local="clr-namespace:WebSiteRatings" xmlns:vm="clr-namespace:WebSiteRatings.ViewModels" xmlns:VoteControl="clr-namespace:StarVoteControl;assembly=StarVoteControl" xmlns:wpfControls="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf" Loaded="MetroWindow_Loaded" mc:Ignorable="d" Title="My Sites" Height="650" Width="1000">
    2. Down where the <Border> element is declared, remove the WebBrowser control and replace it with the wpfControls:WebView2 control:

      <Border Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" BorderThickness="1" BorderBrush="Black" Margin="5"> <wpfControls:WebView2 x:Name="browser" ScrollViewer.CanContentScroll="True" /></Border>
  3. Edit the MainWindow.xaml.cs code behind file. Update the ListBox_SelectionChanged method to set the browser.Source property to a valid Uri. This code previously passed in the website URL as a string, but the WebView2 control requires a Uri.

    private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e){ var siteCollection = (ViewModels.SiteCollection)DataContext; if (siteCollection.SelectedSite != null) browser.Source = new Uri(siteCollection.SelectedSite.Url); else browser.NavigateToString("<body></body>");}

Depending on which version of Windows a user of your app is running, they may need to install the WebView2 runtime. For more information, see Get started with WebView2 in WPF apps.

Modernize: appsettings.json

.NET Framework uses the App.config file to load settings for your app, such as connection strings and logging providers. .NET now uses the appsettings.json file for app settings. App.config files are supported in .NET through the System.Configuration.ConfigurationManager NuGet package, and support for appsettings.json is provided by the Microsoft.Extensions.Configuration NuGet package.

As other libraries upgrade to .NET, they modernize by supporting appsettings.json instead of App.config. For example, logging providers in .NET Framework that have been upgraded for .NET 6+ no longer use App.config for settings. It's good to follow their direction and also move away from using App.config where you can.

Use appsettings.json with the WPF sample app

As an example, after upgrading the WPF sample app, use appsettings.json for the connection string to the local database.

  1. Remove the System.Configuration.ConfigurationManager NuGet package.

  2. Add the Microsoft.Extensions.Configuration.Json NuGet package.

  3. Add a file to the project named appsettings.json.

  4. Set the appsettings.json file to copy to the output directory.

    Set the copy to output setting through Visual Studio using the Properties window after selecting the file in the Solution Explorer. Alternatively you can edit the project directly and add the following ItemGroup:

     <ItemGroup> <Content Include="appsettings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup>
  5. Migrate the settings in the App.config file to a new appsettings.json file.

    In the WPF sample app, app.config only contained a single connection string. Edit the appsettings.json file to define the connection string:

    { "ConnectionStrings": { "database": "DataSource=sqlite.db;" }}
  6. Edit the App.xaml.cs file, instancing a configuration object that loads the appsettings.json file, the added lines are highlighted:

    using System.Windows;using Microsoft.Extensions.Configuration;namespace WebSiteRatings{ /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { public static IConfiguration Config { get; private set; } public App() { Config = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); } }}
  7. In the .\Models\Database.cs file, change the OpenConnection method to use the new App.Config property. This requires importing the Microsoft.Extensions.Configuration namespace:

    using Microsoft.Data.Sqlite;using System.Collections.Generic;using Microsoft.Extensions.Configuration;namespace WebSiteRatings.Models{ internal class Database { public static SqliteConnection OpenConnection() => new SqliteConnection(App.Config.GetConnectionString("database")); public static IEnumerable<Site> ReadSites()

    GetConnectionString is an extension method provided by the Microsoft.Extensions.Configuration namespace.

FAQs

How do I upgrade my solution to .NET 7? ›

Step by step upgrade
  1. Install Upgrade Assistant Visual Studio extension.
  2. In Visual Studio in Solution Explorer right-click on the project you want to upgrade, choose Upgrade.
  3. You will see the main page with a few options for your upgrade. ...
  4. For this example, I choose In-place.
Feb 15, 2023

How do I move a project from .NET 6 to .NET 7? ›

To adopt all of the new 7.0 features for Blazor apps, we recommend the following process:
  1. Create a new 7.0 Blazor project from one of the Blazor project templates. ...
  2. Move the app's components and code to the 7.0 app making modifications to adopt the new 7.0 features.
Mar 12, 2023

Does .NET 7 support WPF? ›

NET 7 support is available for the following product lines/platforms: WinForms Controls. WPF Controls.

Should we upgrade to .NET 7? ›

NET 7 SDK improves the CLI template experience, enables publishing to containers, and central package management with NuGet. Publish to a container:Container images are now a supported output type of the . NET SDK, and you can create containerized versions of your applications using dotnet publish.

How do I upgrade my project from .NET 5 to .NET 7? ›

Migrating from .

NET 5. If this is the case, then in Visual Studio, simply right-click on your project in Solution Explorer and choose Properties. Under Application > General > Target framework, choose . NET 7.0.

Is .NET 7 backwards compatible with 6? ›

Net 7 is backwards compatible with . Net 6.

How do I upgrade .NET version of project? ›

NET versions with Upgrade Assistant right from Visual Studio. Right click on your project file and let the tool guide you through the process. The tool will choose the best upgrade type for your application and upgrade your project file and your code to accommodate some breaking changes and to use newer features.

How do I upgrade to the latest version of NET Framework? ›

NET Framework version. To update the target framework for all projects, right-click on the project (one by one), click properties, and from the “Application” tab change the target framework to the desired one as in the following screenshot and select “Yes” in the popup that is displayed after the framework is changed.

Is WPF still relevant 2023? ›

WPF is still a viable technology, and you can use the latest version of . NET. Existing applications can be modernized and use the power and performance benefits new .

What is replacing WPF? ›

What is the best alternative for WPF? It's the VCL. VCL – an abbreviation of Visual Component Library – is a set of visual tools effective for the rapid development of Windows applications. It uses the easy to learn yet modern, powerful Delphi language for the program code and to drive the applications.

What are the changes in WPF in .NET 7? ›

WPF in . NET 7 ships with number of improvements in the areas not just limited to unnecessary boxing/unboxing, use of Span for string manipulation, better allocation/deallocation of objects, memory improvements, font rendering etc. but also code cleanup and making way for future readiness.

How much faster is .NET 7? ›

Here it is extremly prominent, how much of an improvement the new . NET 7 implementation is demonstrating. We can see an improvement of over 4500% relative to . NET 6!

Why is .NET 7 faster? ›

NET 7 runtime provides us with the best of both worlds, as it allows a fast first JIT compilation as well as fast throughput after a somewhat delayed, slower but highly optimized JIT re-compilation. This can make our . NET application faster, just by executing it on the new .

Is .NET 7 better than .NET 6? ›

NET 7 seeks to improve developers' productivity by building on the foundation of . NET 6. The new . NET will focus on better support for cloud-native scenarios, as well as tools to help developers upgrade legacy applications.

How do you upgrade a solution? ›

To update the solution, follow these steps:
  1. Open the unmanaged solution in your development environment and create new or add and remove the existing components that you want.
  2. Increment the version number when you export the solution as a managed solution. ...
  3. Apply the upgrade or update in the target environment.
Mar 12, 2023

How do I upgrade Winforms to NET 7? ›

Upgrade the app

Right-click on the MatchingGame project in the Solution Explorer window and select Upgrade: Select In-place project upgrade as the upgrade mode. Select . NET 7.0 for the target framework and select Next.

How do I change my active solution platform? ›

On the menu bar, choose Build > Configuration Manager. In the Active solution platform list, choose a platform for the solution to target, and then choose the Close button.

How to update NET Framework? ›

NET Framework version. To update the target framework for all projects, right-click on the project (one by one), click properties, and from the “Application” tab change the target framework to the desired one as in the following screenshot and select “Yes” in the popup that is displayed after the framework is changed.

References

Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated: 23/11/2023

Views: 5956

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.