改造一个NET4.5的WPF项目到NET6

提示:此文章是记录第三方开源WPF模板由net4.5升级到net6的详细过程

文章目录

前言

一、改造思路?

二、改造步骤

1.手动升级csproj项目文件

2.升级项目中使用到的第三方包

3.改造项目代码里面的Mvvm的模型代码


550c2c52-44af-41c0-abc1-d47f6d12718a


前言

被改造的开源项目:Azai: 一个测试git的wpf项目

改造后的开源项目:Azai: 使用NET6改造的WPF UI模板项目

一、改造思路?

1.手动升级csproj项目文件

2.升级或者替换项目中使用到的第三方包

二、改造步骤

1.手动升级csproj项目文件

代码如下:


  
	  WinExe
	  net6.0-windows
	  enable
	  true
	  logo.ico
  
  
    
    
  
  
    
      Always
    
    
      True
      \
      Always
    
  
  
    
    
    
  
  
    
  
  
    
      True
      True
      Resources.resx
    
  
  
    
      True
      True
      Resources.resx
    
  
  
    
      SettingsSingleFileGenerator
      Settings.Designer.cs
    
  
  
    
      True
      True
      Settings.settings
    
  
  
    
      SettingsSingleFileGenerator
      Settings.Designer.cs
    
  
  
    
      8.0.0
    
    
      6.0.0
    
  

2.升级项目中使用到的第三方包

老项目使用的第三方包:



  
  
  

CommonServiceLocator替换成微软官方的依赖注入组件【Microsoft.Extensions.DependencyInjection】。

MvvmLight和MvvmLightLibs由于nuget上的包一标记成弃用,所以使用新的包【CommunityToolkit.Mvvm】。

AduSkin包直接升级成最新版本的包,就可以了。

3.改造项目代码里面的Mvvm的模型代码

命名空间替换

using GalaSoft.MvvmLight

替换成

using CommunityToolkit.Mvvm.ComponentModel;

 以前继承的模型父类由【ViewModelBase】改成【ObservableObject】;【Set】方法改成【SetProperty】。

详细代码如下:

//using GalaSoft.MvvmLight;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Windows.Media;

namespace AZai.ViewModel
{
    public abstract class TabBase : ObservableObject//ViewModelBase
    {
        private int _tabNumber;
        public int TabNumber
        {
            get => _tabNumber;
            set
            {
                if (_tabNumber != value)
                {
                    SetProperty(ref _tabNumber, value);
                }
            }
        }

        private string _tabName;
        public string TabName
        {
            get => _tabName;
            set
            {
                if (_tabName != value)
                {
                    SetProperty(ref _tabName, value);
                }
            }
        }


        private bool _isPinned;
        public bool IsPinned
        {
            get => _isPinned;
            set
            {
                if (_isPinned != value)
                {
                    SetProperty(ref _isPinned, value);
                }
            }
        }


        private ImageSource _tabIcon;
        public ImageSource TabIcon
        {
            get => _tabIcon;
            set
            {
                if (!Equals(_tabIcon, value))
                {
                    SetProperty(ref _tabIcon, value);
                }
            }
        }
    }
}

 以上就是升级Azai WPF项目到NET6的详细步骤,效果图如下:

改造一个NET4.5的WPF项目到NET6_第1张图片

 改造一个NET4.5的WPF项目到NET6_第2张图片

你可能感兴趣的:(C#,NET,CORE,WPF,WPF项目升级,WPF项目升级到NET6)