在 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

前往官网下载:.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 Framework 的 WPF 项目迁移到基于 .NET Core 3的更多相关文章

  1. 58HouseSearch项目迁移到asp.net core

    前言 58HouseSearch这个项目原本是基于ASP.NET MVC 4写的,开发环境是Windows+VS2015,发布平台是linux+mono+jexus,这样看来整个项目基本已经满足跨平台 ...

  2. 旧 WCF 项目迁移到 asp.net core + gRPC 的尝试

    一个月前,公司的运行WCF的windows服务器down掉了,由于 AWS 没有通知,没有能第一时间发现问题. 所以,客户提出将WCF服务由C#改为JAVA,在Linux上面运行:一方面,AWS对Li ...

  3. [Powershell]使用Msbuild构建基于.NET Framework的WebAPI项目

    查找最高版本的MsBuildTools. 清理缓存垃圾. 还原NuGet包. 构建解决方案. 按项目发布程序到本地. 按项目ZIP打包. <# .NOTES ================== ...

  4. 如何创建一个基于 .NET Core 3 的 WPF 项目

    在 Connect(); 2018 大会上,微软发布了 .NET Core 3 Preview,以及基于 .NET Core 3 的 WPF:同时还发布了 Visual Studio 2019 预览版 ...

  5. Apworks框架实战(六):使用基于Entity Framework的仓储基础结构

    在前面的章节中,我们已经设计了一个简单的领域模型,接下来我们希望能够实现领域模型的持久化及查询.在Apworks中,实现了面向Entity Framework.NHibernate以及MongoDB的 ...

  6. 将 WPF、UWP 以及其他各种类型的旧 csproj 迁移成基于 Microsoft.NET.Sdk 的新 csproj

    原文 将 WPF.UWP 以及其他各种类型的旧 csproj 迁移成基于 Microsoft.NET.Sdk 的新 csproj 写过 .NET Standard 类库或者 .NET Core 程序的 ...

  7. 迁移WPF项目到.NET CORE

    综述 .NET CORE 3.0开始,桌面端支持WPF了.很多.NET FRAMEWORK的项目已经跑了一阵子了,不是很有必要支持.NET CORE,不过最近用一个程序,为了贯彻一些C# 8的特性,需 ...

  8. step6----->往工程中添加spring boot项目------->修改pom.xml使得我的project是基于spring boot的,而非直接基于spring framework

    文章内容概述: spring项目组其实有多个projects,如spring IO platform用于管理external dependencies的版本,通过定义BOM(bill of mater ...

  9. 记将一个大型客户端应用项目迁移到 dotnet 6 的经验和决策

    在经过了两年的准备,以及迁移了几个应用项目积累了让我有信心的经验之后,我最近在开始将团队里面最大的一个项目,从 .NET Framework 4.5 迁移到 .NET 6 上.这是一个从 2016 时 ...

随机推荐

  1. angular5中使用echart的方法

    注意两点安装的版本 安装好后可以参照echart的官网使用 1.实现package.json中安装这两个包 2.index.html中引入 3.在appModule中添加 然后再html中就可以这么使 ...

  2. C#正则过滤HTML标签并保留指定标签的方法

    本文实例讲述了C#正则过滤html标签并保留指定标签的方法.分享给大家供大家参考,具体如下: 这边主要看到一个过滤的功能: public static string FilterHtmlTag(str ...

  3. inputsimulator - Windows Input Simulator

    窗体输入模拟器提供一个基于 win32 SendInput  方法的 模拟键盘鼠标输入的.net 接口.windows 输入模拟器可用于 WPF.windows 窗体和控制台应用程序, 实现模拟任意按 ...

  4. LeetCode--225--用队列实现栈

    问题描述: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队 ...

  5. LeetCode--027--移除元素

    问题描述: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...

  6. thinkphp导出报表

    这是我写的一个方法,这个方法可以直接使用在你的代码上.下面我画红色的就是要修改或者删除的.public function import(){ /*创建PHPEXCLE读取,默认excel2007,最好 ...

  7. 170301、使用Spring AOP实现MySQL数据库读写分离案例分析

    使用Spring AOP实现MySQL数据库读写分离案例分析 原创 2016-12-29 徐刘根 Java后端技术 一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案 ...

  8. OAF中多语言的实现(转)

    正好前两天研究过这个问题,分享一下啊. 标题:        OAF中多语言的实现概述:        OAF的多语言的实现有两种方式,其一是直接通过页面上面的“个性化”连接,连接到指定的页面后,进行 ...

  9. Spring Data Rest如何暴露ID字段

    package com.example.demo.config; import com.example.demo.model.Comp; import com.example.demo.model.P ...

  10. 关于PermGen space内存溢出错误解决方法

    1.参考: http://blog.csdn.net/fox009/article/details/5633007 http://hi.baidu.com/like_dark/blog/item/19 ...