Reputation: 522
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?
Upvotes: 2
Views: 3021
Reputation: 12276
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
Upvotes: 2
Reputation: 17298
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.
Upvotes: 2