提示:此文章是记录第三方开源WPF模板由net4.5升级到net6的详细过程
550c2c52-44af-41c0-abc1-d47f6d12718a
改造后的开源项目:Azai: 使用NET6改造的WPF UI模板项目
1.手动升级csproj项目文件
2.升级或者替换项目中使用到的第三方包
代码如下:
"Microsoft.NET.Sdk"> -
-
WinExe -
net6.0-windows -
enable -
true -
logo.ico -
-
-
"Image\cov.png" /> -
"logo.ico" /> -
-
-
"Image\cov.png"> -
Always -
-
"logo.ico"> -
True -
\ -
Always -
-
-
-
"AduSkin" Version="1.1.1.9" /> -
"CommunityToolkit.Mvvm" Version="8.0.0" /> -
"Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> -
-
-
"..\ChromeTabs\ChromeTabs.csproj" /> -
-
-
"Properties\Resources.resx"> -
True -
True -
Resources.resx -
-
-
-
"Properties\Resources.Designer.cs"> -
True -
True -
Resources.resx -
-
-
-
"Properties\Settings.settings"> -
SettingsSingleFileGenerator -
Settings.Designer.cs -
-
-
-
"Properties\Settings.Designer.cs"> -
True -
True -
Settings.settings -
-
-
-
"Properties\Settings.settings"> -
SettingsSingleFileGenerator -
Settings.Designer.cs -
-
-
"'$(TargetFramework)' == 'netcoreapp3.1'"> -
"CommunityToolkit.Mvvm"> -
8.0.0 -
-
"System.Management"> -
6.0.0 -
-
老项目使用的第三方包:
- "1.0" encoding="utf-8"?>
- <packages>
- <package id="CommonServiceLocator" version="2.0.2" targetFramework="net45" />
- <package id="MvvmLight" version="5.4.1.1" targetFramework="net45" />
- <package id="MvvmLightLibs" version="5.4.1.1" targetFramework="net45" />
- packages>
CommonServiceLocator替换成微软官方的依赖注入组件【Microsoft.Extensions.DependencyInjection】。
MvvmLight和MvvmLightLibs由于nuget上的包一标记成弃用,所以使用新的包【CommunityToolkit.Mvvm】。
AduSkin包直接升级成最新版本的包,就可以了。
命名空间替换
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的详细步骤,效果图如下: