当前位置: 首页 > news >正文

将基于 .NET Framework 的 WPF 项目迁移到基于 .NET Core 3

在 Connect(); 2018 大会上,微软发布了 .NET Core 3 Preview,以及基于 .NET Core 3 的 WPF;同时还发布了 Visual Studio 2019 预览版。你可以基于 .NET Core 3 创建 WPF 程序。不过,如果你已经有基于 .NET Framework 的 WPF 项目,那么如何快速迁移到基于 .NET Core 的版本呢?

本文将指导大家将现有基于 .NET Framework 的 WPF 项目迁移到基于 .NET Core 3 的版本。


本文内容

      • 安装 .NET Core 3.0 Preview SDK
      • 编辑 csproj 文件
      • 编辑 AssemblyInfo.cs 文件
      • 恢复 NuGet 包
      • 编译、运行和修复其他错误
      • 更多

安装 .NET Core 3.0 Preview SDK

前往官网下载:.NET Core 3.0 downloads for Linux, macOS, and Windows。

然后安装。

编辑 csproj 文件

卸载你原有的 WPF 项目,然后右键“编辑 csproj 文件”。将里面所有的内容改为以下代码:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>

    <!-- 如果你的项目是 Exe,则设为 WinExe;如果是 WPF 类库,则删掉这一行 -->
    <OutputType>WinExe</OutputType>

    <!-- 如果你的原有项目中有 App.manifest 文件,则在此加入 -->
    <!-- <ApplicationManifest>Properties\App.manifest</ApplicationManifest> -->

    <!-- 如果你的原有项目中有 App.ico 图标,则在此加入 -->
    <!-- <ApplicationIcon>Properties\App.ico</ApplicationIcon> -->

    <!-- 如果你的原有项目中有自定义的 Main 函数,则在此加入 -->
    <!-- <StartupObject>Walterlv.Whitman.Program</StartupObject> -->
  </PropertyGroup>
  <ItemGroup>

    <!-- 如果你的原有项目中有自己添加的图标文件,则在此加入 -->
    <Resource Include="Properties\App.ico" />

    <!-- 如果你的原有项目中有其他非 .cs、.xaml 文件,则需要在这里加入 -->

  </ItemGroup>
</Project>

编辑 AssemblyInfo.cs 文件

由于在 .NET Core 中,程序集相关的信息是自动生成的,所以原有 AssemblyInfo.cs 中的大量程序集信息是需要删掉的,不然会出现重复 Attribute 的错误。

看以下代码,红色标记 “–” 的代码是需要删掉的,其他的代码保留。

--  using System.Reflection;
--  using System.Resources;
--  using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Windows;
    
--  // General Information about an assembly is controlled through the following
--  // set of attributes. Change these attribute values to modify the information
--  // associated with an assembly.
--  [assembly: AssemblyTitle("Whitman")]
--  [assembly: AssemblyDescription("")]
--  [assembly: AssemblyConfiguration("")]
--  [assembly: AssemblyCompany("")]
--  [assembly: AssemblyProduct("Whitman")]
--  [assembly: AssemblyCopyright("Copyright © walterlv 2018")]
--  [assembly: AssemblyTrademark("")]
--  [assembly: AssemblyCulture("")]
--  
    // Setting ComVisible to false makes the types in this assembly not visible
    // to COM components.  If you need to access a type in this assembly from
    // COM, set the ComVisible attribute to true on that type.
    [assembly: ComVisible(false)]
    
--  //In order to begin building localizable applications, set
--  //<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
--  //inside a <PropertyGroup>.  For example, if you are using US english
--  //in your source files, set the <UICulture> to en-US.  Then uncomment
--  //the NeutralResourceLanguage attribute below.  Update the "en-US" in
--  //the line below to match the UICulture setting in the project file.
--  
--  //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
--  
--  
    [assembly: ThemeInfo(
        ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
                                         //(used if a resource is not found in the page,
                                         // or application resource dictionaries)
        ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
                                                  //(used if a resource is not found in the page,
                                                  // app, or any theme specific resource dictionaries)
    )]
--  
--  
--  // Version information for an assembly consists of the following four values:
--  //
--  //      Major Version
--  //      Minor Version
--  //      Build Number
--  //      Revision
--  //
--  // You can specify all the values or you can default the Build and Revision Numbers
--  // by using the '*' as shown below:
--  // [assembly: AssemblyVersion("1.0.*")]
--  [assembly: AssemblyVersion("1.0.0.0")]
--  [assembly: AssemblyFileVersion("1.0.0.0")]

恢复 NuGet 包

打开你原有项目的 packages.config 文件。这里记录了你的项目中已经安装的 NuGet 包。

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Toolkit.Wpf.UI.XamlHost" version="5.0.0" targetFramework="net471" />
</packages>

我们需要把这个文件里面的内容转换成 PackageReference。按照如下的方式逐一将 package 转换成 PackageReference

<PackageReference Include="Microsoft.Toolkit.Wpf.UI.XamlHost" Version="5.0.0" />

这时,csproj 项目文件的内容如下:

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
      <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
        <UseWPF>true</UseWPF>
        <OutputType>WinExe</OutputType>
        <ApplicationManifest>Properties\App.manifest</ApplicationManifest>
        <ApplicationIcon>Properties\App.ico</ApplicationIcon>
        <StartupObject>Walterlv.Whitman.Program</StartupObject>
      </PropertyGroup>
++    <ItemGroup>
++      <PackageReference Include="Microsoft.Toolkit.Wpf.UI.XamlHost" Version="5.0.0" />
++    </ItemGroup>
      <ItemGroup>
        <Resource Include="Properties\App.ico" />
      </ItemGroup>
    </Project>

如果你觉得这一步骤比较繁琐,那么可以在本文一开始就按照这篇博客的方式进行操作:自动将 NuGet 包的引用方式从 packages.config 升级为 PackageReference - walterlv。

编译、运行和修复其他错误

对于比较简单的项目,在经过以上步骤之后,你可能已经可以可以直接跑起来了。

运行

对于复杂一些的项目,你可能会遇到其他的编译或运行错误,你需要适当进行一些修复。而产生这些错误的原因是 csproj 文件中删除了太多的东西。你需要将 <ItemGroup /> 中的一些没有默认添加进来的文件加入进来。

更多

如果你只是希望创建基于 .NET Core 3 的新 WPF 项目,那么请阅读我的另一篇博客:如何创建一个基于 .NET Core 3 的 WPF 项目。

相关文章:

  • 了解 .NET 的默认 TaskScheduler 和线程池(ThreadPool)设置,避免让 Task.Run 的性能急剧降低
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • 在有 UI 线程参与的同步锁(如 AutoResetEvent)内部使用 await 可能导致死锁
  • 不要使用 Dispatcher.Invoke,因为它可能在你的延迟初始化 LazyT 中导致死锁
  • 定义一组抽象的 Awaiter 的实现接口,你下次写自己的 await 可等待对象时将更加方便
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
  • .NET 中什么样的类是可使用 await 异步等待的?
  • Visual Studio 2017 以前的旧格式的 csproj Import 进来的 targets 文件有时不能正确计算属性(PropertyGroup)和集合(ItemGroup)
  • 使用 ReSharper,输入即遵循 StyleCop 的代码格式化规范
  • StyleCop 是什么,可以帮助团队带来什么价值?
  • 文件和文件夹不存在的时候,FileSystemWatcher 监听不到文件的改变?如果递归地监听就可以了
  • C#/.NET 使用 CommandLineParser 来标准化地解析命令行
  • .NET 中使用 TaskCompletionSource 作为线程同步互斥或异步操作的事件
  • 使用 WPF 开发一个 Windows 屏幕保护程序
  • 在 Windows 10 中开启移动 WLAN 热点
  • @jsonView过滤属性
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • 【RocksDB】TransactionDB源码分析
  • 3.7、@ResponseBody 和 @RestController
  • 4个实用的微服务测试策略
  • CentOS 7 修改主机名
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • JavaScript函数式编程(一)
  • learning koa2.x
  • 规范化安全开发 KOA 手脚架
  • 记一次用 NodeJs 实现模拟登录的思路
  • 设计模式(12)迭代器模式(讲解+应用)
  • 手写双向链表LinkedList的几个常用功能
  • 网络应用优化——时延与带宽
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • 浅谈sql中的in与not in,exists与not exists的区别
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • (最优化理论与方法)第二章最优化所需基础知识-第三节:重要凸集举例
  • ***检测工具之RKHunter AIDE
  • .360、.halo勒索病毒的最新威胁:如何恢复您的数据?
  • .Net 4.0并行库实用性演练
  • .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
  • .net web项目 调用webService
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .NET/C# 获取一个正在运行的进程的命令行参数
  • .NET命名规范和开发约定
  • .Net小白的大学四年,内含面经
  • .net知识和学习方法系列(二十一)CLR-枚举
  • .Net中wcf服务生成及调用
  • .NET中的十进制浮点类型,徐汇区网站设计
  • .net中生成excel后调整宽度
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • @reference注解_Dubbo配置参考手册之dubbo:reference
  • @RunWith注解作用
  • [20160902]rm -rf的惨案.txt