受欢迎的博客标签

In this post, I will describe how to port a desktop application from .NET Framework to .NET Core. 

Solution Folder

tools 
└───vs 2019
OS/.NET SDK & .NET  Runtime
├───.NET SDK 5.0
├───.NET 5.0.0  Runtime
│   └───ASP.NET Core Runtime 5.0   <--- The ASP.NET Core Runtime enables you to run existing
Old/New WinForm.csproj 
├───OldFormsApp
│   └───OldForms.csproj
└───NewFormsAppCore
    └───NewFormsCore.csproj

step 1: installing the latest preview of Visual Studio 2019 

https://visualstudio.microsoft.com/vs/preview/

Thank you for downloading Visual Studio
Your download will start shortly. If your download does not begin, click here to retry

 

step 2: install the latest .NET   SDK.

for example

SDK 5.0.100-preview.3
Full version
5.0.100-preview.3.20216.6
Visual Studio support
Visual Studio 2019 (v16.6 - latest preview)
Included runtimes
.NET Runtime 5.0.0-preview.3.20214.6
ASP.NET Core Runtime 5.0.0-preview.3.20214.6
Desktop Runtime 5.0.0-preview.3.20214.6
OS/.NET SDK & .NET  Runtime
├───ASP.NET Core Runtime 5.0   <--- The ASP.NET Core Runtime enables you to run existing web/server applications.
├───Desktop Runtime 5.0            <--- The Desktop Runtime enables you to run existing Windows desktop applications.
└───.NET Runtime 5.0                  <---  The .NET Runtime enables you to run existing  console app.

 

SolutionFolder
├───MyApps.sln
├───MyFormsApp
│   └───MyForms.csproj
└───MyFormsAppCore
    └───MyFormsCore.csproj

https://dotnet.microsoft.com/download/dotnet/5.0?utm_source=dotnet-website&utm_medium=banner&utm_campaign=preview5-banner

SolutionFolder
├───.NET SDK 5.0
├───.NET 5.0.0  Runtime
│   └───ASP.NET Core Runtime 5.0   <--- The ASP.NET Core Runtime enables you to run existing web/server 
└───MyFormsAppCore
    └───MyFormsCore.csproj

OSFolder
├───ASP.NET Core Runtime 5.0   <--- The ASP.NET Core Runtime enables you to run existing web/server applications.
├───Desktop Runtime 5.0            <--- The Desktop Runtime enables you to run existing Windows desktop applications.
└───.NET Runtime 5.0                  <---  The .NET Runtime enables you to run existing  console app.

step 3:replace existing  .csproj project file

To move my application to .NET Core, first I need to change my project file to SDK-style format because the old format does not support .NET Core. 

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp5.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\DataProvider\Common\Common.csproj" />
  </ItemGroup>

</Project>

step 4:NuGet package reference

old winform for .net Framework

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

new form for Net core

path:/.csproj

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

 

Windows Forms Controls 

The following Windows Forms controls have been removed from .NET Core

DataGrid
ToolBar
ContextMenu
Menu
MainMenu
MenuItem

The following replacements are recommended:

Old Control (API) Recommended Replacement Other associated APIs removed
DataGrid DataGridView DataGridCell, DataGridRow, DataGridTableCollection, DataGridColumnCollection, DataGridTableStyle, DataGridColumnStyle, DataGridLineStyle, DataGridParentRowsLabel, DataGridParentRowsLabelStyle, DataGridBoolColumn, DataGridTextBox, GridColumnStylesCollection, GridTableStylesCollection, HitTestType
ToolBar ToolStrip ToolBarAppearance
ToolBarButton ToolStripButton ToolBarButtonClickEventArgs, ToolBarButtonClickEventHandler, ToolBarButtonStyle, ToolBarTextAlign
ContextMenu ContextMenuStrip  
Menu ToolStripDropDown, ToolstripDropDownMenu MenuItemCollection
MainMenu MenuStrip  
MenuItem ToolstripMenuItem  

 

 

 

https://devblogs.microsoft.com/dotnet/how-to-port-desktop-applications-to-net-core-3-0/

https://docs.microsoft.com/en-us/dotnet/core/porting/winforms

https://www.grapecity.com/blogs/winforms-and-dotnet-core-3

https://www.syncfusion.com/blogs/post/syncfusion-winforms-wpf-controls-in-net-core-3.aspx

https://cloud.tencent.com/developer/article/1554211

 https://devblogs.microsoft.com/dotnet/announcing-net-core-3-1/

https://www.zhangshengrong.com/p/l51gwGyW10/

 

Blog ---------- root
 ├── .dockerignore ---------- docker ignore
 ├── .gitattributes ---------- git attributes
 ├── .gitignore ---------- git ignore
 ├── common.props ---------- common.props
 ├── LICENSE ---------- LICENSE
 ├── Meowv.Blog.sln ---------- Solution
 ├── README.md ---------- README.md
 ├── .github ---------- github config
 ├── src
 │   ├── Meowv.Blog.Application ---------- 应用服务层
 │   ├── Meowv.Blog.Application.Caching ---------- 应用服务缓存
 │   ├── Meowv.Blog.Application.Contracts ---------- 应用服务数据传输对象(DTO)
 │   ├── Meowv.Blog.BackgroundJobs ---------- 后台定时任务
 │   ├── Meowv.Blog.Domain ---------- 领域层,实体,仓储接口
 │   ├── Meowv.Blog.Domain.Shared ---------- 领域层,一些常量,枚举等
 │   ├── Meowv.Blog.EntityFrameworkCore ---------- 集成EF Core,仓储接口实现
 │   ├── Meowv.Blog.EntityFrameworkCore.DbMigrations ---------- EF Core数据库迁移
 │   ├── Meowv.Blog.HttpApi ---------- API控制器
 │   ├── Meowv.Blog.HttpApi.Hosting ---------- WebApi项目,依赖于HttpApi,
 │   ├── Meowv.Blog.Swagger ---------- Swagger扩展、Filter
 │   └── Meowv.Blog.ToolKits ---------- 公共的工具类、扩展方法
 └── static ---------- 用于README.md展示图片的图片文件夹