将基于 .NET Framework 的 WPF 项目迁移到基于 .NET Core 3
版權(quán)聲明:本作品采用知識共享署名-非商業(yè)性使用-相同方式共享 4.0 國際許可協(xié)議進(jìn)行許可。歡迎轉(zhuǎn)載、使用、重新發(fā)布,但務(wù)必保留文章署名呂毅(包含鏈接:http://blog.csdn.net/wpwalter/),不得用于商業(yè)目的,基于本文修改后的作品務(wù)必以相同的許可發(fā)布。如有任何疑問,請與我聯(lián)系(walter.lv@qq.com)。 https://blog.csdn.net/WPwalter/article/details/84848052
在 Connect(); 2018 大會上,微軟發(fā)布了 .NET Core 3 Preview,以及基于 .NET Core 3 的 WPF;同時還發(fā)布了 Visual Studio 2019 預(yù)覽版。你可以基于 .NET Core 3 創(chuàng)建 WPF 程序。不過,如果你已經(jīng)有基于 .NET Framework 的 WPF 項目,那么如何快速遷移到基于 .NET Core 的版本呢?
本文將指導(dǎo)大家將現(xiàn)有基于 .NET Framework 的 WPF 項目遷移到基于 .NET Core 3 的版本。
本文內(nèi)容
- 安裝 .NET Core 3.0 Preview SDK
- 編輯 csproj 文件
- 編輯 AssemblyInfo.cs 文件
- 恢復(fù) NuGet 包
- 編譯、運(yùn)行和修復(fù)其他錯誤
- 更多
安裝 .NET Core 3.0 Preview SDK
前往官網(wǎng)下載:.NET Core 3.0 downloads for Linux, macOS, and Windows。
然后安裝。
編輯 csproj 文件
卸載你原有的 WPF 項目,然后右鍵“編輯 csproj 文件”。將里面所有的內(nèi)容改為以下代碼:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"><PropertyGroup><TargetFramework>netcoreapp3.0</TargetFramework><UseWPF>true</UseWPF><!-- 如果你的項目是 Exe,則設(shè)為 WinExe;如果是 WPF 類庫,則刪掉這一行 --><OutputType>WinExe</OutputType><!-- 如果你的原有項目中有 App.manifest 文件,則在此加入 --><!-- <ApplicationManifest>Properties\App.manifest</ApplicationManifest> --><!-- 如果你的原有項目中有 App.ico 圖標(biāo),則在此加入 --><!-- <ApplicationIcon>Properties\App.ico</ApplicationIcon> --><!-- 如果你的原有項目中有自定義的 Main 函數(shù),則在此加入 --><!-- <StartupObject>Walterlv.Whitman.Program</StartupObject> --></PropertyGroup><ItemGroup><!-- 如果你的原有項目中有自己添加的圖標(biāo)文件,則在此加入 --><Resource Include="Properties\App.ico" /><!-- 如果你的原有項目中有其他非 .cs、.xaml 文件,則需要在這里加入 --></ItemGroup> </Project>編輯 AssemblyInfo.cs 文件
由于在 .NET Core 中,程序集相關(guān)的信息是自動生成的,所以原有 AssemblyInfo.cs 中的大量程序集信息是需要刪掉的,不然會出現(xiàn)重復(fù) Attribute 的錯誤。
看以下代碼,紅色標(biāo)記 “–” 的代碼是需要刪掉的,其他的代碼保留。
-- 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")]恢復(fù) NuGet 包
打開你原有項目的 packages.config 文件。這里記錄了你的項目中已經(jīng)安裝的 NuGet 包。
<?xml version="1.0" encoding="utf-8"?> <packages><package id="Microsoft.Toolkit.Wpf.UI.XamlHost" version="5.0.0" targetFramework="net471" /> </packages>我們需要把這個文件里面的內(nèi)容轉(zhuǎn)換成 PackageReference。按照如下的方式逐一將 package 轉(zhuǎn)換成 PackageReference。
<PackageReference Include="Microsoft.Toolkit.Wpf.UI.XamlHost" Version="5.0.0" />這時,csproj 項目文件的內(nèi)容如下:
<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>如果你覺得這一步驟比較繁瑣,那么可以在本文一開始就按照這篇博客的方式進(jìn)行操作:自動將 NuGet 包的引用方式從 packages.config 升級為 PackageReference - walterlv。
編譯、運(yùn)行和修復(fù)其他錯誤
對于比較簡單的項目,在經(jīng)過以上步驟之后,你可能已經(jīng)可以可以直接跑起來了。
對于復(fù)雜一些的項目,你可能會遇到其他的編譯或運(yùn)行錯誤,你需要適當(dāng)進(jìn)行一些修復(fù)。而產(chǎn)生這些錯誤的原因是 csproj 文件中刪除了太多的東西。你需要將 <ItemGroup /> 中的一些沒有默認(rèn)添加進(jìn)來的文件加入進(jìn)來。
更多
如果你只是希望創(chuàng)建基于 .NET Core 3 的新 WPF 項目,那么請閱讀我的另一篇博客:如何創(chuàng)建一個基于 .NET Core 3 的 WPF 項目。
posted on 2018-12-22 22:55 NET未來之路 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/lonelyxmas/p/10162792.html
總結(jié)
以上是生活随笔為你收集整理的将基于 .NET Framework 的 WPF 项目迁移到基于 .NET Core 3的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文件的修改
- 下一篇: ASP.NET MVC+JQueryEa