原創(chuàng)|使用教程|編輯:龔雪|2021-07-05 10:31:18.630|閱讀 229 次
概述:本文主要介紹如何將使用DevExpress控件的WPF應用程序升級到 .NET,切換目標平臺后,您就可以利用新框架的高級功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
本文主要介紹如何將使用DevExpress控件的WPF應用程序升級到 .NET,切換目標平臺后,您就可以利用新框架的高級功能。
DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務應用程序。通過DevExpress WPF能創(chuàng)建有著強大互動功能的XAML基礎應用程序,這些應用程序專注于當代客戶的需求和構建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產品,還是以數(shù)據(jù)為中心的商業(yè)智能產品,都能通過DevExpress WPF控件來實現(xiàn)。
.NET遺漏了.NET Framework 中存在的一部分API,使用 工具確定您的應用程序是否使用此類 API。
嘗試重構您的代碼以減少對缺失 API 的調用次數(shù),尋找具有所需功能的替代API。
檢查項目中使用的NuGet包是否與.NET兼容,以及是否有更新的兼容版本可用。
如有必要,更新您的項目來引用最新的包版本。
TIP:即使Visual Studio未顯示任何編譯時錯誤,也要執(zhí)行此步驟。 如果使用未針對.NET運行時測試的包構建應用程序,則可能會遇到運行時異常。
要將WPF應用程序從.NET Framework遷移到.NET,請按照以下步驟操作:
TIP:遷移前備份您的項目。
.NET 僅適用新的SDK樣式的項目文件格式。
有兩種方法可以創(chuàng)建這樣的項目文件:
將現(xiàn)有項目文件轉換為新格式
使用第三方工具將您的項目文件轉換為 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
該工具顯示遷移步驟列表,如有必要,您可以跳過某些步驟。 升級項目后,重新加載解決方案。
手動創(chuàng)建項目文件
使用下面的代碼示例作為模板來創(chuàng)建新的 *.csproj (*.vbproj) 文件,根據(jù)說明修改文件。
*.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 //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 WPF NuGet包:
將包添加到項目后,Visual Studio 將加載并在工具箱中顯示 DevExpress WPF 控件。
您的項目現(xiàn)在面向 .NET。
如果 Visual Studio 顯示生成錯誤,請按照輸出重構您的代碼,直到您可以成功編譯它。
運行并測試升級后的應用程序。 請記住,即使應用程序是在沒有編譯時錯誤的情況下構建的,運行時異常也是可能的。
DevExpress技術交流群4:715863792 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網(wǎng)