DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

DevExpress WPF入门教程:将WPF应用程序迁移到 .NET

来源:   发布时间:2021-07-05   浏览:1094次

本文主要介绍如何将使用DevExpress控件的WPF应用程序升级到 .NET,切换目标平台后,您就可以利用新框架的高级功能。

DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。

DevExpress WPF v21.1完整版下载

先决条件
考虑便携性限制

.NET遗漏了.NET Framework 中存在的一部分API,使用 .NET Portability Analyzer 工具确定您的应用程序是否使用此类 API。

尝试重构您的代码以减少对缺失 API 的调用次数,寻找具有所需功能的替代API。

更新NuGet包

检查项目中使用的NuGet包是否与.NET兼容,以及是否有更新的兼容版本可用。

如有必要,更新您的项目来引用最新的包版本。

TIP:即使Visual Studio未显示任何编译时错误,也要执行此步骤。 如果使用未针对.NET运行时测试的包构建应用程序,则可能会遇到运行时异常。

迁移应用程序
迁移概述

要将WPF应用程序从.NET Framework迁移到.NET,请按照以下步骤操作:

  1. 将应用程序的项目文件(*.csproj 或 *.vbproj)转换为 SDK 样式格式(或创建新的 SDK 样式项目文件)。
  2. 将应用程序的依赖项(NuGet 包)升级到支持 .NET 的最新版本。
  3. 将目标框架更改为 .NET。
  4. 参考DevExpress控件使用NuGet源替代全局程序集缓存 (GAC)。
  5. 查看并修复在编译和运行时出现的错误和异常。

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, see
https://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="\Images\MyAppImage1.png" />
<Resource Include="\Images\MyAppImage2.png" />
<!-- You need to include the Content sections from the old project file -->
</ItemGroup>

<!--
If your project depends on NuGet packages, the solution contains
the `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包:

  1. 注册本地或在线DevExpress NuGet feed
  2. 查找并安装 DevExpress.WindowsDesktop.Wpf 和 DevExpress.WindowsDesktop.Wpf.Themes.All 包

将包添加到项目后,Visual Studio 将加载并在工具箱中显示 DevExpress WPF 控件。

您的项目现在面向 .NET。

修复构建问题

如果 Visual Studio 显示生成错误,请按照输出重构您的代码,直到您可以成功编译它。

  1. 使用Microsoft.Windows.Compatibility 包访问特定于Windows的API:Registry, WCF Client, ACL等。
  2. 使用conditional compilation 编写针对.NET和.NET Framework不同的一小部分代码。
测试您的应用

运行并测试升级后的应用程序。 请记住,即使应用程序是在没有编译时错误的情况下构建的,运行时异常也是可能的。


DevExpress技术交流群4:715863792      欢迎一起进群讨论

更多DevExpress线上公开课、中文教程资讯请上中文网获取

慧都高端UI界面开发
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/2510.html

相关产品: DevExpress Universal Subscription,

在线
客服
微信
QQ 电话
023-68661681
返回
顶部