怎么迁移Windows Forms应用到.NET 5

原文:https://docs.microsoft.com/zh-cn/dotnet/desktop/winforms/migration/?view=netdesktop-5.0

前提

  • Visual Studio 2019 version 16.8,我用的是 Visual Studio 2019 version 16.8.1
    • 安装.net 5;
    • 安装桌面开发;
  • 打开 Use the preview Windows Forms designer for .NET Core apps 设置;我的VS默认就是打开的

思考

进行迁移前必须考虑下面这几点;

  1. .NET Portability Analyzer 检查你的应用是否适合迁移;
  2. 是否有不同步的Windows Forms版本,如果使用.NET Core 3.0版本的Forms,将很难迁移;
  3. Windows Compatibility Pack 会帮助我们进行迁移;PS:这是一个适配层。
  4. 更新项目使用的NuGet包;

备份项目

pass

NuGet包

packages.config点击右键,选择 将packages.config 迁移到 ProjectReference

项目文件

  1. 卸载项目;

  2. 编辑项目文件;

  3. 把打开的项目文件内容拷贝到其他地方;

  4. 删除所有内容,并复制下面代码;

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net5.0-windows</TargetFramework>
        <UseWindowsForms>true</UseWindowsForms>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
      </PropertyGroup>
    
    </Project>
    

    注意:如果是库项目不需要 <OutputType>设置。

接下来就进行项目文件的具体内容迁移:

  1. 把 下列元素从旧项目文件中拷贝到 新的项目的<PropertyGroup>属性中:

    • <RootNamespace>

    • <AssemblyName>

    最终结果如下:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net5.0-windows</TargetFramework>
        <UseWindowsForms>true</UseWindowsForms>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    
        <RootNamespace>MatchingGame</RootNamespace>
        <AssemblyName>MatchingGame</AssemblyName>
      </PropertyGroup>
    
    </Project>
    
  2. 复制旧项目文件中所有的到新项目文件中:

    这一步操作后,结果如下:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        (contains settings previously described)
      </PropertyGroup>
    
      <ItemGroup>
        <ProjectReference Include="..\MatchingGame.Logic\MatchingGame.Logic.csproj">
          <Project>{36b3e6e2-a9ae-4924-89ae-7f0120ce08bd}</Project>
          <Name>MatchingGame.Logic</Name>
        </ProjectReference>
      </ItemGroup>
      <ItemGroup>
        <PackageReference Include="MetroFramework">
          <Version>1.2.0.3</Version>
        </PackageReference>
      </ItemGroup>
    
    </Project>
    

    注意:标签不需要 和属性。

    <ItemGroup>
      <ProjectReference Include="..\MatchingGame.Logic\MatchingGame.Logic.csproj" />
    </ItemGroup>
    

资源和设置

经典的基于.NET Framework项目包含些其他文件,如: Properties/Settings.settingsProperties/Resources.resx,这些文件也需要迁移。

方法是:把旧项目的所有<ItemGroup>元素都复制过来,修改中的IncludeUpdate

  • Settings.settings 文件
<ItemGroup>
  <None Include="Properties\Settings.settings">
    <Generator>SettingsSingleFileGenerator</Generator>
    <LastGenOutput>Settings.Designer.cs</LastGenOutput>
  </None>
  <Compile Update="Properties\Settings.Designer.cs">
    <AutoGen>True</AutoGen>
    <DependentUpon>Settings.settings</DependentUpon>
    <DesignTimeSharedInput>True</DesignTimeSharedInput>
  </Compile>
</ItemGroup>
  • 资源类文件,如:properties/Resources.resx,要把从移除。

    <ItemGroup>
      <EmbeddedResource Update="Properties\Resources.resx">
        <Generator>ResXFileCodeGenerator</Generator>
        <LastGenOutput>Resources.Designer.cs</LastGenOutput>
      </EmbeddedResource>
      <Compile Update="Properties\Resources.Designer.cs">
        <AutoGen>True</AutoGen>
        <DependentUpon>Resources.resx</DependentUpon>
        <DesignTime>True</DesignTime>
      </Compile>
    </ItemGroup>
    

编辑App.config文件

如果你的项目有这个文件,就删除文件中的元素。

添加适配层(compatibility package)

部分项目可能缺少API或者直接是包,这个时候尝试添加 Microsoft.Windows.Compatibility

测试项目

到现在为止,已经完整的迁移了整个项目。测试迁移结果。

PS:经过我测试,一个Hello Word Form 项目,单文件大小约110MB(ZIP压缩后60MB),裁剪不使用的库后约70MB(ZIP压缩后30MB)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值