将Windows窗体移植到.Net Standard 2.0 [英] Porting Windows Forms to .Net Standard 2.0

查看:116
本文介绍了将Windows窗体移植到.Net Standard 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,这是一个非常幼稚的问题.我前一段时间使用.Net 4.5编写了Windows窗体应用程序.最近,我认为使用VS Code将其移植到.Net Standard 2.0应用程序是一个好主意.

Apologies if this is a very naive question. I wrote a Windows Form application using .Net 4.5 sometime ago. Recently, I thought it would be a good idea to port it to a .Net Standard 2.0 application using VS Code.

缺少库和类有很多问题(System.ServiceModel是最大的差距),但是我已经成功地构建了应用程序.但是,当我运行它时,看到以下错误:

There were a couple of problems with missing libraries and classes (System.ServiceModel being the biggest gap), but I have got to the point of building the application successfully. However, when I come to run it, I see the following error:

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058) ---> System.BadImageFormatException: Cannot load a reference assembly for execution.

如果有用,这里是项目文件:

Here's the project file if it's useful:

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

<ItemGroup>
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="Microsoft.CSharp" Version="4.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="NUnit" Version="3.8.1" />
<PackageReference Include="System.CodeDom" Version="4.4.0" />
<PackageReference Include="System.Configuration" Version="2.0.5" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.4.0" />
<PackageReference Include="System.Net.Http" Version="4.3.3" />
<PackageReference Include="System.ServiceModel" Version="1.0.0" />
<PackageReference Include="System.ServiceModel.Security" Version="4.4.0" />
<PackageReference Include="System.Windows.Forms" Version="4.0.0.0" />
</ItemGroup>

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

</Project>

是否可以使用System.Windows.Forms库运行该应用程序,还是应该将其替换为.Net Standard 2.0的其他库?

Is there some way to run the application using the System.Windows.Forms library, or should I replace it with a different library for .Net Standard 2.0?

推荐答案

您不能构建针对.NET Standard 2.0的应用程序.只有库可以针对.NET Standard.应用程序仍然必须针对特定的运行时-.NET Framework,.NET Core,Xamarin等.

You cannot build an application that targets .NET Standard 2.0. Only libraries can target .NET Standard. Applications still have to target a specific runtime - .NET Framework, .NET Core, Xamarin, etc.

从您的项目文件来看,您实际上是在瞄准.NET Core 2.0.

Judging by your project file, you're actually targeting .NET Core 2.0.

System.Windows.Forms是.NET Framework(和Mono)的一部分.它不作为.NET Standard或.NET Core的一部分存在.

System.Windows.Forms is part of the .NET Framework (and Mono). It does not exist as part of .NET Standard or .NET Core.

您的项目文件的System.Windows.Forms具有PackageReference,它将拉入

Your project file has a PackageReference to System.Windows.Forms, which will pull in this NuGet package. It's unofficial and unlisted. It's very description is "unsupported library".

该程序包中的DLL只是一个参考程序集,即所有方法均不包含任何实际代码.该程序集仅包含定义.

The DLL inside that package is only a reference assembly, i.e. none of the methods contain any actual code. That assembly only contains definitions.

这是错误消息表示不应加载参考程序集以执行"的意思.该程序包中的程序集将允许您进行编译,但是没有其他内容.

This is what the error message means by "Reference assemblies should not be loaded for execution." The assembly in that package will let you compile, but nothing else.

没有正式的方法可以在.NET Core(3.0之前的版本)上运行Windows Forms应用程序.将我们的业务逻辑放入.NET Standard程序集中可能会有好处,但是您的用户界面仍然必须是.NET Framework应用程序.

There is no formal way to run a Windows Forms application on .NET Core (prior to 3.0). There may be benefit to pulling our your business logic into a .NET Standard assembly, but your user interface will still have to be a .NET Framework application.

这篇关于将Windows窗体移植到.Net Standard 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆