环境
首先要下载.NET Core 3.0 与Visual Studio 2019
编辑 csproj 文件
1
2
3
4
5
6
7
8
|
< Project Sdk = "Microsoft.NET.Sdk.WindowsDesktop" > < PropertyGroup > < OutputType >WinExe</ OutputType > < TargetFramework >netcoreapp3.0</ TargetFramework > < UseWindowsForms >true</ UseWindowsForms > < GenerateAssemblyInfo >false</ GenerateAssemblyInfo > </ PropertyGroup > </ Project > |
恢复 NuGet 包
找到package.config文件右击选择Upgrade to PackageReference 但是我会弹框
我也不知道该怎么解决,搜了一圈没搜到,如果有知道的可以在评论区告诉我,于是我就老老实实将
1
2
3
4
5
6
7
8
9
10
|
<? xml version = "1.0" encoding = "utf-8" ?> < packages > < package id = "Dapper" version = "1.50.2" targetFramework = "net45" /> < package id = "EntityFramework" version = "6.0.0" targetFramework = "net45" /> < package id = "MySql.Data" version = "6.7.9" targetFramework = "net45" /> < package id = "System.Data.SQLite" version = "1.0.108.0" targetFramework = "net45" /> < package id = "System.Data.SQLite.Core" version = "1.0.108.0" targetFramework = "net45" /> < package id = "System.Data.SQLite.EF6" version = "1.0.108.0" targetFramework = "net45" /> < package id = "System.Data.SQLite.Linq" version = "1.0.108.0" targetFramework = "net45" /> </ packages > |
改为下列代码,并写到csproj文件中在 project的子节点中
1
2
3
4
5
6
7
8
9
|
< ItemGroup > < PackageReference Include = "Dapper" Version = "1.50.2" /> < PackageReference Include = "EntityFramework" Version = "6.0.0" /> < PackageReference Include = "MySql.Data" Version = "6.7.9" /> < PackageReference Include = "System.Data.SQLite" Version = "1.0.108.0" /> < PackageReference Include = "System.Data.SQLite.Core" Version = "1.0.108.0" /> < PackageReference Include = "System.Data.SQLite.EF6" Version = "1.0.108.0" /> < PackageReference Include = "System.Data.SQLite.Linq" Version = "1.0.108.0" /> </ ItemGroup > |
增加引用
同时,有些dll是 .net framework独有,而 .net core因为各种原因并没有引用进来的。参考Porting to .NET Core。如以下几个是需要重新引用的:
- System.Data。虽然基础层已经是 .NET Core的一部分,即提供者模型和SQL客户端,但目前还没有一些功能,例如架构支持和DataTable/ DataSet。
- System.DirectoryServices。.NET Core目前不支持与LDAP或Active Directory通信。
- System.Drawing。虽然严格来说它是客户端API,但许多开发人员在服务器上使用绘图API来提供缩略图生成或水印。我们目前在 .NET Core中不支持这些API。
- System.Transactions。虽然ADO.NET支持事务,但不支持分布式事务,其中包括环境事务和登记的概念。
- System.Xml.Xsl和System.Xml.Schema。.NET Core支持XmlDocumentLinq XDocument,包括XPath。但是,目前不支持XSD(XmlSchema)或XSLT(XslTransform)。
- System.Net.Mail。目前不支持使用这些API从 .NET Core发送电子邮件。
- System.IO.Ports。.NET Core目前不包括与串行端口通信的功能。
- System.Workflow。Windows Workflow Foundation(WF)目前在 .NET Core上不可用。
- System.Xaml。在创建UWP应用程序时,开发人员将使用WinRT XAML API。因此,.NET Core目前不包含托管XAML框架,该框架包括解析XAML文档和实例化描述的对象图的能力。
在我的项目中,我引用了这些:
1
2
3
4
5
6
7
8
9
|
< ItemGroup > < Reference Include = "System.Data" /> < Reference Include = "System.Xaml" /> < Reference Include = "System.Transactions" /> < Reference Include = "System.Drawing" /> < Reference Include = "System.Data.DataSetExtensions" /> < Reference Include = "Microsoft.CSharp" /> < Reference Include = "System.Net.Http" /> </ ItemGroup > |
增加其他文件
增加了如下几个项目本身就有的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< ItemGroup > < None Include = "App_Data\data.db" /> < None Include = "packages.config" /> < None Include = "Properties\Settings.settings" > < Generator >SettingsSingleFileGenerator</ Generator > < LastGenOutput >Settings.Designer.cs</ LastGenOutput > </ None > </ ItemGroup > < ItemGroup > < None Include = "App.config" > < SubType >Designer</ SubType > </ None > </ ItemGroup > |
最终的csproj文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
< Project Sdk = "Microsoft.NET.Sdk.WindowsDesktop" > < PropertyGroup > < OutputType >WinExe</ OutputType > < TargetFramework >netcoreapp3.0</ TargetFramework > < UseWindowsForms >true</ UseWindowsForms > < GenerateAssemblyInfo >false</ GenerateAssemblyInfo > </ PropertyGroup > < ItemGroup > < PackageReference Include = "Dapper" Version = "1.50.2" /> < PackageReference Include = "EntityFramework" Version = "6.0.0" /> < PackageReference Include = "MySql.Data" Version = "6.7.9" /> < PackageReference Include = "System.Data.SQLite" Version = "1.0.108.0" /> < PackageReference Include = "System.Data.SQLite.Core" Version = "1.0.108.0" /> < PackageReference Include = "System.Data.SQLite.EF6" Version = "1.0.108.0" /> < PackageReference Include = "System.Data.SQLite.Linq" Version = "1.0.108.0" /> </ ItemGroup > < ItemGroup > < Reference Include = "System.Data" /> < Reference Include = "System.Xaml" /> < Reference Include = "System.Transactions" /> < Reference Include = "System.Drawing" /> < Reference Include = "System.Data.DataSetExtensions" /> < Reference Include = "Microsoft.CSharp" /> < Reference Include = "System.Net.Http" /> </ ItemGroup > < ItemGroup > < None Include = "App_Data\data.db" /> < None Include = "packages.config" /> < None Include = "Properties\Settings.settings" > < Generator >SettingsSingleFileGenerator</ Generator > < LastGenOutput >Settings.Designer.cs</ LastGenOutput > </ None > </ ItemGroup > < ItemGroup > < None Include = "App.config" > < SubType >Designer</ SubType > </ None > </ ItemGroup > </ Project > |
移植过程中遇到的问题
.net core不支持Access数据库
我的代码中有带Access数据库相关代码,都报了缺少程序集引用的错误,因此我初步增加了引用
1
2
3
4
|
< ItemGroup > < Reference Include = "System.Data" /> < Reference Include = "System.Data.OleDb" /> </ ItemGroup > |
但是还是没用,参考了.NET Core Data Access,发现是.net core不支持Access的原因,因此都把这段代码用条件编译代码 #if 给包起来了。
缺少hostfxr.dll文件
编译成功后运行项目发现弹这个窗口,我查了下C:\Program Files\dotnet文件中是存在hostfxr.dll的,看了环境变量也是手动设置了DOTNET_ROOT变量的,但是总是出错,没办法,最后通过复制hostfxr.dll文件到bin\Debug\netcoreapp3.0\下才运行成功
到此这篇关于如何将Winform移植到.NET Core 3.0的文章就介绍到这了,更多相关Winform移植到.NET Core 3.0内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/m0_37080285/article/details/90211658