我试图在Visual Studio代码2017中创建(简单)Windows窗体应用程序。可悲的是我无法使用完整的VS版本,所以我没有用于创建新项目的菜单选项。在C#中使用`dotnet new`创建Windows窗体应用程序
此前,我已经通过在VSC中的终端运行dotnet new console
来创建控制台项目。但是,我无法得到这个与表单应用程序一起工作。
以下扩展名从市场安装:
我试了一下:
创建控制台应用程序,并引用using System.Windows.Forms;
:但是,这需要我来引用这个命名空间:
The type or namespace 'Forms' does not exist in the namespace "System.Windows"
所以我试图用NuGet Package Manager: Add Package
命令添加这个命令,但是在这里我找不到包System.Windows.Forms
。我能想到的
最后一件事是手工添加到.csproj的文件的引用:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeIdentifiers>win7-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3"/>
<PackageReference Include="System.Windows.Forms" HintPath = "\..\WINDOWS\Microsoft.NET\Framework\v4.0.30319"/>
</ItemGroup>
</Project>
但运行dotnet restore
时,这给了警告:
warning NU1604: Project dependency System.Windows.Forms does not contain an inclusive lower bound. Include a lower bound in the dependency version to ensure consistent restore results.
warning NU1701: Package 'System.Windows.Forms 4.0.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
而且运行时给出了以下例外:
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.
创建一个.cs文件,并使用csc
进行编译。这样我可以使用Forms
,但我失去了处理其他依赖关系的功能dotnet
。
问题
我觉得这个问题患有XY问题。我是C#和.NET的新手,所以我不确定自己在哪里失败,无论我尝试做甚至是可能的。
所以我的问题是,如何使用dotnet new
命令创建Windows窗体应用程序?
也许这可以帮助:https://stackoverflow.com/documentation/winforms/1018/getting-started-with-winforms/11054/creating- a-simple-c-sharp-winforms-application-using-a-text-editor#t = 201710051003564647005(注意,这是SO文档,可能会迟早消失) –
@PeterB文档已经停止使用,文章没有任何内容这个问题 – EpicKip
@PeterB这就是我用于尝试2.但是,这打破了'Newtonsoft.Json'包的导入。这完全是一个不同的问题。由于这不会直接成为解决方案,所以我很想知道是否可以使用'dotnet'。 – JAD