亲宝软件园·资讯

展开

Winform移植到.NET Core 3.0 怎样将Winform移植到.NET Core 3.0

迷惘小书童 人气:0

参考将现有应用程序移植到.NET Core 3.0

环境

首先要下载.NET Core 3.0Visual Studio 2019

编辑 csproj 文件

<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 但是我会弹框


我也不知道该怎么解决,搜了一圈没搜到,如果有知道的可以在评论区告诉我,于是我就老老实实将

<?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的子节点中

<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。如以下几个是需要重新引用的:

在我的项目中,我引用了这些:

<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>

最终的csproj文件

<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数据库相关代码,都报了缺少程序集引用的错误,因此我初步增加了引用

<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\下才运行成功

加载全部内容

相关教程
猜你喜欢
用户评论