简体   繁体   English

使用`dotnet new`在C#中创建Windows窗体应用程序

[英]Creating a Windows Forms Application in C# using `dotnet new`

I am trying to create a (simple) windows form application in Visual Studio Code 2017. Sadly I am unable to use the full VS versions, so I don't have the menu options to create new projects. 我试图在Visual Studio Code 2017中创建一个(简单的)Windows窗体应用程序。可悲的是我无法使用完整的VS版本,所以我没有菜单选项来创建新项目。

Previously, I have created console projects by running dotnet new console from the terminal in VSC. 以前,我通过在VSC中运行来自终端的dotnet new console来创建控制台项目。 However, I can't get this to work with a forms application. 但是,我无法使用表单应用程序。

The following extensions are installed from the Marketplace: 从Marketplace安装以下扩展程序:

在此输入图像描述

What I tried: 我尝试了什么:

1 1

Create console application, and reference using System.Windows.Forms; using System.Windows.Forms;创建控制台应用程序和引用using System.Windows.Forms; : However, this requires me to reference this namespace: :但是,这需要我引用这个命名空间:

The type or namespace 'Forms' does not exist in the namespace "System.Windows" 命名空间“System.Windows”中不存在类型或命名空间“Forms”

So I tried to add this using the NuGet Package Manager: Add Package command, but here I can't find the package System.Windows.Forms . 所以我尝试使用NuGet Package Manager: Add Package命令,但在这里我找不到包System.Windows.Forms

One last thing I could think of was to manually add a reference to the .csproj file: 我能想到的最后一件事是手动添加对.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>

But when running dotnet restore this gives warnings: 但是当运行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.

And when run gives the following 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.

2 2

Create a single .cs file, and compile that with csc . 创建一个.cs文件,并使用csc编译。 This way I can use Forms , but I lose the functionality of dotnet handling other dependencies. 这样我可以使用Forms ,但是我失去了dotnet处理其他依赖项的功能。

Question

I feel like this issue suffers from the XY problem. 我觉得这个问题会受到XY问题的影响。 I am new to C# and .NET, so I am not sure where I am failing, whether what I am trying to do is even possible. 我是C#和.NET的新手,所以我不确定我失败的地方,我想做的事情是否可能。

So my question is, how do I create a windows forms application using the dotnet new command? 所以我的问题是,如何使用dotnet new命令创建一个Windows窗体应用程序?

This answer is a workaround. 这个答案是一种解决方法。 And a hacky workaround at that. 还有一个hacky解决方法。 If you can, use Visual Studio instead of this. 如果可以,请使用Visual Studio而不是此。

It took a bit (read: lot) of puzzling, but I managed to tie some bits of info left and right together. 花了一些时间(阅读:很多)令人费解,但我设法将一些信息左右联系在一起。

Creating Forms, which framework? 创建表单,哪个框架?

According to this answer on another question , there are different frameworks within .NET which allow the creation of different apps, as shown by this graphic: 根据另一个问题的答案 ,.NET中有不同的框架允许创建不同的应用程序,如下图所示:

.NET框架

Another post on Quora seems to second this point: 关于Quora的另一篇文章似乎是第二点:

All native user interface technologies (Windows Presentation Foundation, Windows Forms, etc) are part of the framework, not the core. 所有本机用户界面技术(Windows Presentation Foundation,Windows Forms等)都是框架的一部分,而不是核心。

This means that while we are using the wrong framework. 这意味着我们正在使用错误的框架。 By default, dotnet new seems to use the .NET CORE framework, as we can see in the .csproj file: 默认情况下, dotnet new似乎使用.NET CORE框架,我们可以在.csproj文件中看到:

<TargetFramework>netcoreapp2.0</TargetFramework>

This is not what we want. 这不是我们想要的。 We want the .NET FRAMEWORK. 我们想要.NET FRAMEWORK。 According to the Microsoft Documentation we can change this to net<versionnumber> . 根据Microsoft文档,我们可以将其更改为net<versionnumber>

Adding the dependency 添加依赖项

The dependency System.Windows.Forms can then be added like so: 然后可以添加依赖System.Windows.Forms如下所示:

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

One last thing 最后一件事

When compiling this, I ran into another compilation error: 编译时,我遇到了另一个编译错误:

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

Which can be easily fixed by adding Microsoft.CSharp to the dependencies using NuGet . 通过使用NuGetMicrosoft.CSharp添加到依赖项,可以轻松修复此问题。

The .csproj file then looks like this: 然后.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>

Starting from dotnet 3.0 you can just run the following command for initializing WinForms Application: 从dotnet 3.0开始,您可以运行以下命令来初始化WinForms应用程序:

dotnet new winforms

For initializing wpf application just run: 对于初始化wpf应用程序,只需运行:

dotnet new wpf

You can see all available project types for dotnet 3.0 by running dotnet new or dotnet new --help (both commands produce the same output). 您可以通过运行dotnet newdotnet new --help (两个命令生成相同的输出)来查看dotnet 3.0的所有可用项目类型。

PS: tested with dotnet 3.0.100-preview-010184 . PS:用dotnet 3.0.100-preview-010184

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

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