先决条件
- .NET
- 安装了 .NET 桌面开发工作负载的Visual Studio 2019 version 16.3
考虑便携性限制
.NET遗漏了.NET Framework 中存在的一部分API,使用 .NET Portability Analyzer 工具确定您的应用程序是否使用此类 API。
尝试重构您的代码以减少对缺失 API 的调用次数,寻找具有所需功能的替代API。
更新NuGet包
检查项目中使用的NuGet包是否与.NET兼容,以及是否有更新的兼容版本可用。
如有必要,更新您的项目来引用最新的包版本。
TIP:即使Visual Studio未显示任何编译时错误,也要执行此步骤。 如果使用未针对.NET运行时测试的包构建应用程序,则可能会遇到运行时异常。
迁移应用程序
迁移概述
要将WPF应用程序从.NET Framework迁移到.NET,请按照以下步骤操作:
- 将应用程序的项目文件(*.csproj 或 *.vbproj)转换为 SDK 样式格式(或创建新的 SDK 样式项目文件)。
- 将应用程序的依赖项(NuGet 包)升级到支持 .NET 的最新版本。
- 将目标框架更改为 .NET。
- 参考DevExpress控件使用NuGet源替代全局程序集缓存 (GAC)。
- 查看并修复在编译和运行时出现的错误和异常。
TIP:迁移前备份您的项目。
创建一个新的项目文件
.NET 仅适用新的SDK样式的项目文件格式。
有两种方法可以创建这样的项目文件:
- 将现有项目文件转换为新格式,这种方法最适合只有少数依赖项的小型应用程序。
- 手动创建项目文件,此手动选项可让您更好地控制迁移过程。
将现有项目文件转换为新格式
使用.NET Upgrade Assistant 第三方工具将您的项目文件转换为 SDK 样式格式并将目标框架更改为 .NET。
在Visual Studio的主菜单中选择View -> Terminal打开集成终端,运行以下命令安装.NET Upgrade Assistant。
.NET CLI
dotnet tool install -g upgrade-assistant
安装.NET Upgrade Assistant后,您可以使用以下命令对其进行更新:
.NET CLI
dotnet tool update -g upgrade-assistant
要将您的解决方案升级到.NET 5,请运行以下命令,指定要转换的项目名称:
.NET CLI
upgrade-assistant upgrade .ProjectName.csproj
该工具显示迁移步骤列表,如有必要,您可以跳过某些步骤。 升级项目后,重新加载解决方案。
手动创建项目文件
使用下面的代码示例作为模板来创建新的 *.csproj (*.vbproj) 文件,根据说明修改文件。
*.csproj
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>WinExe</OutputType><!-- The TargetFramework value may differ --><TargetFramework>net472</TargetFramework><UseWPF>true</UseWPF><!-- The `UseWindowsForms` option is required --><UseWindowsForms>true</UseWindowsForms><!-- AssemblyInfo.cs is generated automatically by default.Disable auto-generation or remove the file. --><GenerateAssemblyInfo>false</GenerateAssemblyInfo><!-- Copy and paste from the old project file --><AssemblyName>MyCoreApp</AssemblyName><!-- Copy and paste from the old project file --><RootNamespace>MyWPF</RootNamespace><!-- Useful for conditional compilation, seehttps://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if --><DefineConstants>NETCORE</DefineConstants><!-- Your application icon --><ApplicationIcon>MyAppIcon.ico</ApplicationIcon></PropertyGroup><!-- ApplicationDefinition, all the C# files, XAML pages, EmbeddedResources,and App.config are included by default,so you do not need to include them manually --><ItemGroup><!-- EmbeddedResources are included by default,but regular resources are not included by default,so you need to include them to your new project file --><Resource Include="MyAppIcon.ico" /><Resource Include="ImagesMyAppImage1.png" /><Resource Include="ImagesMyAppImage2.png" /><!-- You need to include the Content sections from the old project file --></ItemGroup><!--If your project depends on NuGet packages, the solution containsthe `packages.config` file.In the old project, convert packages.config to Package Reference format:right click the package.config file in the solution explorer ->"Migrate packages.config to PackageReference..."Your NuGet packages go here, copy them from the old project file --><ItemGroup><!-- These package references are added here for demonstration purposes.Add the packages your applcation actually uses. --><PackageReference Include="Newtonsoft.Json" Version="12.0.2" /><PackageReference Include="EntityFramework" Version="6.3.0-preview9-19423-04" /><PackageReference Include="Microsoft.Data.SQLite" Version="3.0.0-preview9.19423.6" /></ItemGroup></Project>
在此步骤之后,您的项目仍以.NET Framework为目标,并且具有SDK样式的项目文件,检查项目是否成功构建并运行。
将TargetFramework选项更改为net5.0-windows并将项目的Sdk更改为Microsoft.NET.Sdk:
*.csproj
<!-- Change the Sdk --><Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>WinExe</OutputType><!-- Change the TargetFramework --><TargetFramework>net5.0-windows</TargetFramework><UseWPF>true</UseWPF><UseWindowsForms>true</UseWindowsForms>...
将DevExpress引用从GAC切换到NuGet
添加DevExpress WPF NuGet包:
- 注册本地或在线DevExpress NuGet feed
- 查找并安装 DevExpress.WindowsDesktop.Wpf 和 DevExpress.WindowsDesktop.Wpf.Themes.All 包
将包添加到项目后,Visual Studio 将加载并在工具箱中显示 DevExpress WPF 控件。
您的项目现在面向 .NET。
修复构建问题
如果 Visual Studio 显示生成错误,请按照输出重构您的代码,直到您可以成功编译它。
- 使用Microsoft.Windows.Compatibility 包访问特定于Windows的API:Registry, WCF Client, ACL等。
- 使用conditional compilation 编写针对.NET和.NET Framework不同的一小部分代码。
测试您的应用
运行并测试升级后的应用程序。 请记住,即使应用程序是在没有编译时错误的情况下构建的,运行时异常也是可能的。
DevExpress技术交流群4:715863792 欢迎一起进群讨论

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!