3

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?

2
  • Can you post the error you get when you call dotnet build? Docs: learn.microsoft.com/en-us/dotnet/core/tools/dotnet Also try dotnet --help or dotnet <command> --help for help in the command line. Commented Feb 13, 2023 at 18:31
  • Why are you not creating the wpf project using visual studio? Or at least the dotnet command line. You're not planning on doing all your development in notepad are you? Commented Feb 14, 2023 at 14:36

2 Answers 2

3

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.

enter image description here

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

@user172501 If that solution worked for you, then please accept the answer (click on the checkmark on the left of the text), so that others might find the answer as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.