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


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


    前言

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

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

    一、改造思路?

    1.手动升级csproj项目文件

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

    二、改造步骤

    1.手动升级csproj项目文件

    代码如下:

    1. "Microsoft.NET.Sdk">
    2. WinExe
    3. net6.0-windows
    4. enable
    5. true
    6. logo.ico
    7. "Image\cov.png" />
    8. "logo.ico" />
    9. "Image\cov.png">
    10. Always
    11. "logo.ico">
    12. True
    13. \
    14. Always
    15. "AduSkin" Version="1.1.1.9" />
    16. "CommunityToolkit.Mvvm" Version="8.0.0" />
    17. "Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
    18. "..\ChromeTabs\ChromeTabs.csproj" />
    19. "Properties\Resources.resx">
    20. True
    21. True
    22. Resources.resx
    23. "Properties\Resources.Designer.cs">
    24. True
    25. True
    26. Resources.resx
    27. "Properties\Settings.settings">
    28. SettingsSingleFileGenerator
    29. Settings.Designer.cs
    30. "Properties\Settings.Designer.cs">
    31. True
    32. True
    33. Settings.settings
    34. "Properties\Settings.settings">
    35. SettingsSingleFileGenerator
    36. Settings.Designer.cs
    37. "'$(TargetFramework)' == 'netcoreapp3.1'">
    38. "CommunityToolkit.Mvvm">
    39. 8.0.0
    40. "System.Management">
    41. 6.0.0

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

    老项目使用的第三方包:

    1. "1.0" encoding="utf-8"?>
    2. <packages>
    3. <package id="CommonServiceLocator" version="2.0.2" targetFramework="net45" />
    4. <package id="MvvmLight" version="5.4.1.1" targetFramework="net45" />
    5. <package id="MvvmLightLibs" version="5.4.1.1" targetFramework="net45" />
    6. packages>

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

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

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

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

    命名空间替换

    using GalaSoft.MvvmLight

    替换成

    using CommunityToolkit.Mvvm.ComponentModel;

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

    详细代码如下:

    1. //using GalaSoft.MvvmLight;
    2. using CommunityToolkit.Mvvm.ComponentModel;
    3. using System.Windows.Media;
    4. namespace AZai.ViewModel
    5. {
    6. public abstract class TabBase : ObservableObject//ViewModelBase
    7. {
    8. private int _tabNumber;
    9. public int TabNumber
    10. {
    11. get => _tabNumber;
    12. set
    13. {
    14. if (_tabNumber != value)
    15. {
    16. SetProperty(ref _tabNumber, value);
    17. }
    18. }
    19. }
    20. private string _tabName;
    21. public string TabName
    22. {
    23. get => _tabName;
    24. set
    25. {
    26. if (_tabName != value)
    27. {
    28. SetProperty(ref _tabName, value);
    29. }
    30. }
    31. }
    32. private bool _isPinned;
    33. public bool IsPinned
    34. {
    35. get => _isPinned;
    36. set
    37. {
    38. if (_isPinned != value)
    39. {
    40. SetProperty(ref _isPinned, value);
    41. }
    42. }
    43. }
    44. private ImageSource _tabIcon;
    45. public ImageSource TabIcon
    46. {
    47. get => _tabIcon;
    48. set
    49. {
    50. if (!Equals(_tabIcon, value))
    51. {
    52. SetProperty(ref _tabIcon, value);
    53. }
    54. }
    55. }
    56. }
    57. }

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

     

  • 相关阅读:
    WnvHtmlToPdf-x64-v16.0--Crack
    小家电Type-C接口PD诱骗芯片 6500
    CloudQuery + StarRocks:打造高效、安全的数据库管控新模式
    2066. 账户余额
    java8日期和时间API全解——更完善的日期和时间API
    函数式编程中元组的简单运用
    穿越时空:未来云计算的奇妙世界
    selenium教程(2)CSS元素操作
    net-java-php-python-阿克苏水果销售管理计算机毕业设计程序
    C++虚函数指针(virtual)
  • 原文地址:https://blog.csdn.net/allenwdj/article/details/127118291