简体   繁体   English

如何将旧的 WinForms 项目迁移到新的 VS2017 项目格式

[英]How-to migrate old WinForms projects to the new VS2017 project format

We are currently in the process of upgrading our projects to the new VS2017 project format.我们目前正在将我们的项目升级到新的 VS2017 项目格式。 The main reason behind this was converting some libraries from PCL to .NetStandard and being able to use the new built in Nuget Package features that come with the new project format and therefor getting rid of the whole nuget/library reference/dependency issue.这背后的主要原因是将一些库从 PCL 转换为 .NetStandard,并能够使用新项目格式附带的新内置 Nuget 包功能,从而摆脱整个 nuget/库引用/依赖关系问题。

[Edit] The main reason to migrate all projects in the solution is to prevent problems with not getting correct dependencies in .nupkg when mixing old (still working with .nuspec) and new format projects [编辑] 迁移解决方案中所有项目的主要原因是为了防止在混合旧(仍在使用 .nuspec)和新格式项目时无法在 .nupkg 中获得正确依赖项的问题

The answers to the question How-to migrate Wpf projects to the new VS2017 format helped us a long way in converting the WPF parts of our projects. How-to migrate Wpf projects to the new VS2017 format问题的答案帮助我们转换项目的 WPF 部分。 However, we still have huge amounts of Windows Forms stuff, which can't be replaced or upgraded.但是,我们仍然有大量的 Windows 窗体东西,它们无法替换或升级。

With just converting the project file, adding the proper settings for file dependencies (for *.Designer.cs and *.resx files) we managed to get it to compile.只需转换项目文件,为文件依赖项(*.Designer.cs 和 *.resx 文件)添加适当的设置,我们就设法使其编译。

How can we now declare the project items in a way to make Visual Studio 2017 a) recognize them as either UserControl or Form ?我们现在如何声明项目项以使 Visual Studio 2017 a) 将它们识别为 UserControl 或 Form ? b) open the WinForms designer tool to edit .cs or Designer.cs files accordingly ? b) 打开 WinForms 设计器工具以相应地编辑 .cs 或 Designer.cs 文件?

Any hint towards finding a solution is greatly appreciated.非常感谢找到解决方案的任何提示。

I don't believe the SDK style projects have support for the various designers yet.我不相信 SDK 风格的项目还支持各种设计者。 While you can get the nested files to work with DependentUpon attributes, that's as far as it'll get.虽然您可以让嵌套文件与DependentUpon属性一起使用,但这是它所能得到的。

That said, if your primary aim is to use the new NuGet features and use the transitive NuGet references, the existing project system supports this now.也就是说,如果您的主要目标是使用新的 NuGet 功能并使用可传递的 NuGet 引用,那么现有的项目系统现在支持这一点。

Just remove all of your nuget packages manually then delete the packages.config file.只需手动删除所有 nuget 包,然后删除packages.config文件。 You may then have to set <RestoreProjectStyle>PackageReference</RestoreProjectStyle> in your csproj.然后,您可能必须在 csproj 中设置<RestoreProjectStyle>PackageReference</RestoreProjectStyle> When you do that, the legacy project type should now use PackageReference NuGet references and do transitive restores.执行此操作时,旧项目类型现在应使用PackageReference NuGet 引用并执行传递还原。

Note that only packages are transitive this way, project-to-project references are not, so you'll need p2p refs the same as you did before.请注意,只有包可以通过这种方式传递,项目到项目的引用不是,因此您需要与以前一样的 p2p 引用。

Following the release of .NET Core 3.0, the WinForms is now supported but the designer support is currently scheduled for Q4 2020 and can be previewed using Visual Studio 16.4 Preview 3 or later, you have to set the Tools->Options->Preview Features->Use the Preview of Windows Forms designer for .NET Core apps checkbox for this to be enabled. .NET Core 3.0 发布后,现在支持 WinForms,但设计器支持目前计划于2020 年第四季度,可以使用 Visual Studio 16.4 Preview 3 或更高版本进行预览,您必须设置工具->选项->预览功能->使用 .NET Core 应用程序的 Windows 窗体设计器预览复选框启用此功能。

SDK-style projects should be setup as follows... SDK风格的项目应该设置如下...

<ProjectSdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
</Project>

Before starting suggest you run the Portability Analyzer against the project since there are some known differences/incompatibilities.在开始之前,建议您针对项目运行 Portability Analyzer,因为存在一些已知的差异/不兼容性。 There is also another tool called try-convert which will attempt to convert your project.还有另一个名为try-convert 的工具将尝试转换您的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM