[英]Creating a Windows Forms Application in C# using `dotnet new`
我試圖在Visual Studio Code 2017中創建一個(簡單的)Windows窗體應用程序。可悲的是我無法使用完整的VS版本,所以我沒有菜單選項來創建新項目。
以前,我通過在VSC中運行來自終端的dotnet new console
來創建控制台項目。 但是,我無法使用表單應用程序。
從Marketplace安裝以下擴展程序:
我嘗試了什么:
using System.Windows.Forms;
創建控制台應用程序和引用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.
創建一個.cs文件,並使用csc
編譯。 這樣我可以使用Forms
,但是我失去了dotnet
處理其他依賴項的功能。
我覺得這個問題會受到XY問題的影響。 我是C#和.NET的新手,所以我不確定我失敗的地方,我想做的事情是否可能。
所以我的問題是,如何使用dotnet new
命令創建一個Windows窗體應用程序?
這個答案是一種解決方法。 還有一個hacky解決方法。 如果可以,請使用Visual Studio而不是此。
花了一些時間(閱讀:很多)令人費解,但我設法將一些信息左右聯系在一起。
根據另一個問題的答案 ,.NET中有不同的框架允許創建不同的應用程序,如下圖所示:
關於Quora的另一篇文章似乎是第二點:
所有本機用戶界面技術(Windows Presentation Foundation,Windows Forms等)都是框架的一部分,而不是核心。
這意味着我們正在使用錯誤的框架。 默認情況下, dotnet new
似乎使用.NET CORE框架,我們可以在.csproj文件中看到:
<TargetFramework>netcoreapp2.0</TargetFramework>
這不是我們想要的。 我們想要.NET FRAMEWORK。 根據Microsoft文檔,我們可以將其更改為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'
通過使用NuGet
將Microsoft.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>
從dotnet 3.0開始,您可以運行以下命令來初始化WinForms應用程序:
dotnet new winforms
對於初始化wpf應用程序,只需運行:
dotnet new wpf
您可以通過運行dotnet new
或dotnet new --help
(兩個命令生成相同的輸出)來查看dotnet 3.0的所有可用項目類型。
PS:用dotnet 3.0.100-preview-010184
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.