用.NET从命令行构建WPF应用程序。

huangapple go评论67阅读模式
英文:

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

 &lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;

  &lt;PropertyGroup&gt;
    &lt;OutputType&gt;Exe&lt;/OutputType&gt;
    &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;
    &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
    &lt;Nullable&gt;enable&lt;/Nullable&gt;
  &lt;/PropertyGroup&gt;

&lt;/Project&gt;

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 = &quot;Skortchpard&quot;;

        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>部分的任何位置添加以下行:

&lt;UseWPF&gt;True&lt;/UseWPF&gt;

这将(隐式地)向您的项目添加了System.Windows的引用。因此,这有点特殊,因为大多数其他外部引用通常通过<ProjectReference>条目添加。

还要将以下行:

&lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;

更改为

&lt;TargetFramework&gt;net7.0-windows&lt;/TargetFramework&gt;

因为WPF是特定于Windows的。

英文:

You need to add the line

&lt;UseWPF&gt;True&lt;/UseWPF&gt;

anywhere inside the &lt;PropertyGroup&gt; 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 &lt;ProjectReference&gt; entries).

Oh, and also change

 &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;

to

 &lt;TargetFramework&gt;net7.0-windows&lt;/TargetFramework&gt;

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 应用程序的正确设置。

这是我执行上述命令后得到的结果。

用.NET从命令行构建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 &quot;MyNewApp&quot; -lang &quot;C#&quot;

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.

用.NET从命令行构建WPF应用程序。

If I open in visual studio I can hit f5 and it spins up fine

huangapple
  • 本文由 发表于 2023年2月14日 02:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定