【发布时间】: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