前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >将基于 .NET Framework 的 WPF 项目迁移到基于 .NET Core 3

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

作者头像
walterlv
发布2023-10-22 10:18:41
2690
发布2023-10-22 10:18:41
举报

在 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

然后安装。

如果你没有安装 Visual Studio 2019 Preview,请前往下载:Visual Studio 2019

编辑 csproj 文件

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

<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 的错误。

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

-- 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 包。

1 2 3 4

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

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

1

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

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

<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

添加 Windows 兼容包

如果你原有的 WPF 项目引用了一些注册表等 Windows 特有的功能,那么你还需要引用一个 Windows 兼容 NuGet 包:

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

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

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

更多

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

可以持续关注官方 WPF on .NET Core 的例子:samples/wpf/WPF-WinRT at master · dotnet/samples

本文会经常更新,请阅读原文: https://blog.walterlv.com/post/migrate-wpf-project-from-dotnet-framework-to-dotnet-core.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://blog.walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 ([email protected])

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装 .NET Core 3.0 Preview SDK
  • 编辑 csproj 文件
  • 编辑 AssemblyInfo.cs 文件
  • 恢复 NuGet 包
  • 添加 Windows 兼容包
  • 编译、运行和修复其他错误
  • 更多
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档