2017-10-05 142 views
2

我试图在Visual Studio代码2017中创建(简单)Windows窗体应用程序。可悲的是我无法使用完整的VS版本,所以我没有用于创建新项目的菜单选项。在C#中使用`dotnet new`创建Windows窗体应用程序

此前,我已经通过在VSC中的终端运行dotnet new console来创建控制台项目。但是,我无法得到这个与表单应用程序一起工作。

以下扩展名从市场安装:

enter image description here

我试了一下:

创建控制台应用程序,并引用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窗体应用程序?

+0

也许这可以帮助: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文档,可能会迟早消失) –

+0

@PeterB文档已经停止使用,文章没有任何内容这个问题 – EpicKip

+0

@PeterB这就是我用于尝试2.但是,这打破了'Newtonsoft.Json'包的导入。这完全是一个不同的问题。由于这不会直接成为解决方案,所以我很想知道是否可以使用'dotnet'。 – JAD

回答

2

This answer is a workaround. And a hacky workaround at that. If you can, use Visual Studio instead of this.

它花了点儿(读:很多)令人费解,但我设法将一些信息左右一起。

创建窗体,哪个框架?

根据this answer on another question,其中有不同的框架。NET允许不同的应用程序的创建,如通过这个图形:

.NET frameworks

对Quora的另一个post似乎第二这一点:

All native user interface technologies (Windows Presentation Foundation, Windows Forms, etc) are part of the framework, not the core.

这意味着,当我们使用的是错误的框架。默认情况下,dotnet new似乎使用.NET核心框架,我们可以在.csproj的文件中看到:

<TargetFramework>netcoreapp2.0</TargetFramework> 

这不是我们想要的。我们需要.NET FRAMEWORK。根据Microsoft Documentation,我们可以将其更改为net<versionnumber>

添加依赖

的依赖System.Windows.Forms然后可以加入像这样:

<PackageReference Include="System.Windows.Forms" HintPath = "\..\WINDOWS\Microsoft.NET\Framework\v4.0.30319"/> 

最后一件事

当编译此,我遇到了另一个编译错误:

error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create' 

可以通过添加Microsoft.CSharp使用NuGet来依赖。

的.csproj的文件,则是这样的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Project Sdk="Microsoft.NET.Sdk"> 
    <PropertyGroup> 
    <OutputType>Exe</OutputType> 
    <TargetFramework>net452</TargetFramework> 
    </PropertyGroup> 
    <ItemGroup> 
    <PackageReference Include="Newtonsoft.Json" Version="10.0.2"/> 
    <PackageReference Include="System.Windows.Forms" HintPath="\..\WINDOWS\Microsoft.NET\Framework\v4.0.30319"/> 
    <PackageReference Include="Microsoft.CSharp" Version="4.4.0"/> 
    </ItemGroup> 
</Project> 
+0

您应该回到在Windows上使用Visual Studio。在VSCode中进行WinForms/WPF开发并不现实。在Microsoft正式支持这个新项目系统中的WinForms/WPF之前,它可能有效,但也可能会中断。例如,'System.Windows.Forms'可能是'Reference'而不是'PackageReference'。 –

+0

@LexLi是的,我知道这是一个hacky解决方法。由于授权问题,我无法使用VS 2017,所以目前这是我做到这一点的唯一方法。我会补充答案,这不是做这件事的好方法。 – JAD

+0

如果授权是使用Visual Studio的问题,您应该发现SharpDevelop通常足够用于小型项目。 –

相关问题