系统相关
首页 > 系统相关> > c# – 将Windows窗体移植到.Net Standard 2.0

c# – 将Windows窗体移植到.Net Standard 2.0

作者:互联网

如果这是一个非常天真的问题,请道歉.我之前用.Net 4.5编写了一个Windows Form应用程序.最近,我认为使用VS Code将它移植到.Net Standard 2.0应用程序是个好主意.

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

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.

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

<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的不同库替换它?

解决方法:

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

根据您的项目文件判断,您实际上是以.NET Core 2.0为目标.

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

您的项目文件有一个PackageReference到System.Windows.Forms,它将引入this NuGet包.这是非正式的,不公开的.它的描述是“不受支持的库”.

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

这是错误消息的含义“不应加载参考程序集以执行”.该包中的程序集将允许您编译,但没有别的.

没有正式的方法可以在.NET Core上运行Windows窗体应用程序.将我们的业务逻辑拉入.NET标准程序集可能会有好处,但您的用户界面仍然必须是.NET Framework应用程序.

标签:c,net,winforms,net-standard-2-0
来源: https://codeday.me/bug/20190611/1216696.html