将基于 .NET Framework 的 WPF 项目迁移到基于 .NET Core 3
版權聲明:本作品采用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可。歡迎轉載、使用、重新發布,但務必保留文章署名呂毅(包含鏈接:http://blog.csdn.net/wpwalter/),不得用于商業目的,基于本文修改后的作品務必以相同的許可發布。如有任何疑問,請與我聯系(walter.lv@qq.com)。 https://blog.csdn.net/WPwalter/article/details/84848052
在 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
- 編輯 csproj 文件
- 編輯 AssemblyInfo.cs 文件
- 恢復 NuGet 包
- 編譯、運行和修復其他錯誤
- 更多
安裝 .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 項目。
posted on 2018-12-22 22:55 NET未來之路 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/lonelyxmas/p/10162792.html
總結
以上是生活随笔為你收集整理的将基于 .NET Framework 的 WPF 项目迁移到基于 .NET Core 3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文件的修改
- 下一篇: face recognition[Mob