【问题标题】:Creating a Windows Forms Application in C# using `dotnet new`使用 `dotnet new` 在 C# 中创建 Windows 窗体应用程序
【发布时间】:2017-10-05 09:58:54
【问题描述】:

我正在尝试在 Visual Studio Code 2017 中创建一个(简单的)窗体应用程序。遗憾的是,我无法使用完整的 VS 版本,因此我没有创建新项目的菜单选项。

以前,我通过在 VSC 中的终端运行 dotnet new console 创建了控制台项目。但是,我无法让它与表单应用程序一起使用。

从 Marketplace 安装以下扩展程序:

我尝试了什么:

1

创建控制台应用程序,并引用using System.Windows.Forms;:但是,这需要我引用这个命名空间:

命名空间“System.Windows”中不存在类型或命名空间“Forms”

所以我尝试使用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.

2

创建一个 .cs 文件,并使用 csc 编译它。这样我可以使用Forms,但我失去了dotnet处理其他依赖项的功能。

问题

我觉得这个问题受到 XY 问题的影响。我是 C# 和 .NET 的新手,所以我不确定我在哪里失败,我想要做的事情是否可行。

所以我的问题是,如何使用 dotnet new 命令创建 Windows 窗体应用程序?

【问题讨论】:

  • 也许这会有所帮助:stackoverflow.com/documentation/winforms/1018/…(注意,这是在 SO 文档上,迟早会消失)
  • @PeterB 文档已经停产,帖子与问题无关
  • @PeterB 这就是我用于尝试 2 的内容。但是,这会破坏 Newtonsoft.Json 包的导入。这将是一个完全不同的问题。由于这不会直接成为解决方案,我很想知道是否可以使用dotnet
  • 请查看 Visual Studio 2017 安装程序,显示您已安装的 Visual Studio 功能。否则,这个问题无法回答。
  • @LexLi 这是关于 Visual Studio Code,而不是 VS 2017。它不是通过您提到的安装功能选择安装的。

标签: c# .net winforms visual-studio-code


【解决方案1】:

从 dotnet 3.0 开始,您只需运行以下命令即可初始化 WinForms 应用程序:

dotnet new winforms

要初始化 wpf 应用程序,只需运行:

dotnet new wpf

您可以通过运行 dotnet newdotnet new --help 来查看 dotnet 3.0 的所有可用项目类型(这两个命令产生相同的输出)。

P.S.:用 dotnet 3.0.100-preview-010184 测试。

【讨论】:

    【解决方案2】:

    此答案是一种解决方法。还有一个 hacky 解决方法。如果可以,请使用 Visual Studio 代替它。

    花了一点(阅读:很多)令人费解的,但我设法将一些信息左右联系在一起。

    创建表单,哪个框架?

    根据this answer on another question,.NET 中有不同的框架允许创建不同的应用程序,如下图所示:

    Quora 上的另一个 post 似乎支持这一点:

    所有本机用户界面技术(Windows Presentation Foundation、Windows 窗体等)都是框架的一部分,而不是核心。

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

    <TargetFramework>netcoreapp2.0</TargetFramework>
    

    这不是我们想要的。我们想要.NET FRAMEWORK。根据Microsoft Documentation,我们可以把它改成net&lt;versionnumber&gt;

    添加依赖

    然后可以像这样添加依赖项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'
    

    这可以通过使用NuGetMicrosoft.CSharp 添加到依赖项中轻松解决。

    .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>
    

    【讨论】:

    • 您应该返回在 Windows 上使用 Visual Studio。在 VSCode 中进行 WinForms/WPF 开发是不现实的方法。在微软在这个新的项目系统中正式支持 WinForms/WPF 之前,它可能会工作,但也可能会崩溃。例如,System.Windows.Forms 可能是 Reference 而不是 PackageReference
    • @LexLi 是的,我知道这是一个 hacky 解决方法。由于许可问题,我无法使用 VS 2017,所以目前这是我唯一的方法。我会补充说这不是一个好方法。
    • 如果许可是使用 Visual Studio 的问题,您应该会发现 SharpDevelop 通常对于小型项目来说已经足够了。
    • @LexLi 谢谢,我去看看
    猜你喜欢
    • 2018-12-06
    • 2014-12-17
    • 2018-04-24
    • 1970-01-01
    • 2023-02-10
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多