英文:
Build WPF application with .net from the command line
问题
我想学习如何使用C#构建工具从命令行构建桌面应用程序。
我刚刚从这里下载了.NET SDK,并成功运行了我的第一个控制台应用程序,按照这里的教程。
但是,如果我想构建一个WPF应用程序,它会显示“System.Windows”类型或命名空间不可用,并且可能缺少对一个程序集的引用。
我的项目文件如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
我的C#文件如下所示:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Ink;
public class Sketchpad : Application {
[STAThread]
public static void Main(){
var app = new Sketchpad();
Window root = new Window();
InkCanvas inkCanvas1 = new InkCanvas();
root.Title = "Skortchpard";
root.ResizeMode = ResizeMode.CanResizeWithGrip;
inkCanvas1.Background = Brushes.DarkSlateBlue;
inkCanvas1.DefaultDrawingAttributes.Color = Colors.SpringGreen;
inkCanvas1.DefaultDrawingAttributes.Height = 10;
inkCanvas1.DefaultDrawingAttributes.Width = 10;
root.Content = inkCanvas1;
root.Show();
app.MainWindow = root;
app.Run();
}
}
我需要如何更改项目文件以包括必要的程序集?
对于Windows Forms应用程序,应该如何处理?
要从控制台编译C#程序,应该阅读哪个教程?
英文:
I want to learn to use the c# build tools to build desktop applications from the command line.
I just downloaded the .Net SDK from here
and managed to run my first console application following the tutorial from here
However, if i want to build a wpf application, it says type or namespace "System.Windows" is not available and there is a reference to an assembly possibly missing.
My project file looks like this
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
My C# file looks like this:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Ink;
public class Sketchpad : Application {
[STAThread]
public static void Main(){
var app = new Sketchpad();
Window root = new Window();
InkCanvas inkCanvas1 = new InkCanvas();
root.Title = "Skortchpard";
root.ResizeMode = ResizeMode.CanResizeWithGrip;
inkCanvas1.Background = Brushes.DarkSlateBlue;
inkCanvas1.DefaultDrawingAttributes.Color = Colors.SpringGreen;
inkCanvas1.DefaultDrawingAttributes.Height = 10;
inkCanvas1.DefaultDrawingAttributes.Width = 10;
root.Content = inkCanvas1;
root.Show();
app.MainWindow = root;
app.Run();
}
}
How do I need to change the project file to include the necessary assemblies?
How would it be for a Windows Forms-Application?
What tutorial should you read, in order to compile C# programs from the console?
答案1
得分: 2
需要在您的.csproj文件的<PropertyGroup>
部分的任何位置添加以下行:
<UseWPF>True</UseWPF>
这将(隐式地)向您的项目添加了System.Windows
的引用。因此,这有点特殊,因为大多数其他外部引用通常通过<ProjectReference>
条目添加。
还要将以下行:
<TargetFramework>net7.0</TargetFramework>
更改为
<TargetFramework>net7.0-windows</TargetFramework>
因为WPF是特定于Windows的。
英文:
You need to add the line
<UseWPF>True</UseWPF>
anywhere inside the <PropertyGroup>
section of your .csproj file. That (implicitly) adds the System.Windows
references to your project. (As such, this is kind of a special case, as most other external references would be added via <ProjectReference>
entries).
Oh, and also change
<TargetFramework>net7.0</TargetFramework>
to
<TargetFramework>net7.0-windows</TargetFramework>
because WPF is windows-specific.
答案2
得分: 1
你可以使用命令行来创建一个 WPF 项目,使用 dotnet new
,类似于那个教程。当然,除了一些参数告诉它你想要一个 WPF 应用程序以及使用什么语言。
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-new
在 .NET 7 中,你可以这样做:
dotnet new wpf --name "MyNewApp" -lang "C#"
这将在你当前命令行所在的文件夹内创建一个名为 MyNewApp 的文件夹,其中包含一个 MyNewApp.csproj 文件。
这个 csproj 文件包含了一个 WPF 应用程序的正确设置。
这是我执行上述命令后得到的结果。
如果我在 Visual Studio 中打开它,我可以按 F5 键,它将正常运行。
英文:
You can use command line to create a wpf project using dotnet new rather like that tutorial. Except of course with some parameters tell it you want a wpf app and what language.
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-new
With .net 7 you can do:
dotnet new wpf --name "MyNewApp" -lang "C#"
That will create a folder called MyNewApp with a MyNewApp.csproj within whichever folder you're currently "in" with your command line.
The csproj has the correct settings for a wpf app.
This is what I get when I do that.
If I open in visual studio I can hit f5 and it spins up fine
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论