How to create a dotnet core WPF application using dotnet-cli

NET Core command-line interface (dotnet-cli) is a new NET Core toolset (commands) for developing NET Core Applications. The dotnet-cli toolset provides a list of pre-installed dotnet core project templates from which the user can create various applications as a dotnet core WPF (Windows Presentation Foundation) application, console application, Web application, Windows Form application, and other test applications. The application created with dotnet-cli can be executed on any Windows, Linux, or macOS operating system.

How to create a “dotnet core WPF application”

The user can use dotnet new “WPF Application” or “dotnet new wpf” command from the dotnet-CLI toolset to create a new dotnet core WPF (Windows Presentation Foundation) application. The command provides a few options that can be applied as a parameter to tweak the output of the command as shown below :

“dotnet new WPF –no-restore”: This command does not restore the required project dependencies by the WPF application. Please follow through to read about a few “dotnet new WPF” command usage.

Prerequisite:

.NET Core SDK 3.0 (preview) and above. The current version is .NET Core 5 and can be downloaded here:

Command Syntax

The format of the command is as follows: dotnet new wpf [options]

The details of the available command options can be listed using the –help option as follows:

c:\Temp\dotnet>dotnet new wpf --help
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.


WPF Application (C#)
Author: Microsoft
Description: A project for creating a .NET Core WPF Application
Options:
  --langVersion  Sets langVersion in the created project file
                 text - Optional

  --no-restore   If specified, skips the automatic restore of the project on create.
                 bool - Optional
                 Default: false / (*) true


* Indicates the value used if the switch is provided without a value.

Command Usage- dotnet new wpf:

1. “dotnet new wpf”

The command creates a new “NET Core WPF” project. The name of the project is not specified in the command so by default the project name will be the same as the directory where the command was executed, additionally, the command will also restore the dependency required by the project implicitly. The default language is C#, so the project created will be of C# type. The list of the file that will be created using the mentioned commands is given below.

dotnet core WPF
c:\dotnet>dotnet new wpf
The template "WPF Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on c:\dotnet\dotnet.csproj...
  Restore completed in 134.16 ms for c:\dotnet\dotnet.csproj.
Restore succeeded.

The files generated by the command:

c:\dotnet>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8
 Directory of c:\dotnet

06/14/2019  04:57 PM    <DIR>          .
06/14/2019  04:57 PM    <DIR>          ..
06/14/2019  04:57 PM               366 App.xaml
06/14/2019  04:57 PM               339 App.xaml.cs
06/14/2019  04:57 PM               224 dotnet.csproj
06/14/2019  04:57 PM               491 MainWindow.xaml
06/14/2019  04:57 PM               658 MainWindow.xaml.cs
06/14/2019  04:57 PM    <DIR>          obj

c:\dotnet\obj>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of c:\dotnet\obj

06/14/2019  04:57 PM    <DIR>          .
06/14/2019  04:57 PM    <DIR>          ..
06/14/2019  04:57 PM               149 dotnet.csproj.nuget.cache
06/14/2019  04:57 PM             2,220 dotnet.csproj.nuget.dgspec.json
06/14/2019  04:57 PM             1,161 dotnet.csproj.nuget.g.props
06/14/2019  04:57 PM               294 dotnet.csproj.nuget.g.targets
06/14/2019  04:57 PM             2,300 project.assets.json

2. “dotnet new wpf –name Hello”

The command creates a new “NET Core WPF” project having the name of Hello.csproj. The command creates a Hello directory only if doesn’t exist and then creates the boilerplate files in the directory, additionally, the command also restores the dependency required by the project.

c:\>dotnet new wpf --name Hello
The template "WPF Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on Hello\Hello.csproj...
  Restore completed in 98.33 ms for c:\Hello\Hello.csproj.

Restore succeeded.

The files generated by the command:

c:\dotnet>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of c:\dotnet

06/14/2019  04:57 PM    <DIR>          .
06/14/2019  04:57 PM    <DIR>          ..
06/14/2019  04:57 PM               366 App.xaml
06/14/2019  04:57 PM               339 App.xaml.cs
06/14/2019  04:57 PM               224 dotnet.csproj
06/14/2019  04:57 PM               491 MainWindow.xaml
06/14/2019  04:57 PM               658 MainWindow.xaml.cs
06/14/2019  04:57 PM    <DIR>          obj


c:\dotnet\obj>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of c:\dotnet\obj

06/14/2019  04:57 PM    <DIR>          .
06/14/2019  04:57 PM    <DIR>          ..
06/14/2019  04:57 PM               149 dotnet.csproj.nuget.cache
06/14/2019  04:57 PM             2,220 dotnet.csproj.nuget.dgspec.json
06/14/2019  04:57 PM             1,161 dotnet.csproj.nuget.g.props
06/14/2019  04:57 PM               294 dotnet.csproj.nuget.g.targets
06/14/2019  04:57 PM             2,300 project.assets.json

3. “dotnet new wpf –name Hello –no-restore

The command creates a new “NET Core WPF” project having the name of Hello.csproj. The command creates a directory only if doesn’t exist and then creates the boilerplate files in the directory, the command does not restore the dependency required by the project. The output of the command is as follows:

c:\>dotnet new wpf --name Hello --no-restore
The template "WPF Application" was created successfully.

c:\Hello>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 8053-FAC8

 Directory of c:\Hello

06/14/2019  05:11 PM    <DIR>          .
06/14/2019  05:11 PM    <DIR>          ..
06/14/2019  05:11 PM               364 App.xaml
06/14/2019  05:11 PM               338 App.xaml.cs
06/14/2019  05:11 PM               224 Hello.csproj
06/14/2019  05:11 PM               489 MainWindow.xaml
06/14/2019  05:11 PM               657 MainWindow.xaml.cs

4. “dotnet new wpf –name Hello –langVersion 7.0”

The command creates a new “NET Core WPF” project having the name of Hello.csproj. The command creates a Hello directory only if doesn’t exist and then creates the boilerplate files and directory, additionally, the command also restores the dependency required by the project. The command creates the project specific to C# 7.0 (version). The details can be found in the Hello.csproj file.

C:\>dotnet new wpf --name Hello --langVersion 7.0
The template "WPF Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on Hello\Hello.csproj...
  Restore completed in 123.41 ms for C:\Hello\Hello.csproj.

Restore succeeded.

Content of Hello.csproj file: LangVersion tag contains 7.0

C:\Hello>type Hello.csproj

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <LangVersion>7.0</LangVersion>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
</Project>

I hope you found this post on how to create a dotnet core WPF application using dotnet-cli helpful. Thanks for visiting. Cheers !!!

[Further Readings: How to create a dotnet core console app using dotnet-cli |  Top 7 Web Frameworks to Learn and Focus on in 2021 |  Top 7 Programming Languages to Focus on in 2021 |  Creational Design Patterns |   Abstract Factory Design Pattern in C#  |  Factory Method Pattern in C#  |  Singleton Design Pattern in C#  |  Introduction to Design Patterns  |  How to add Git Bash to Windows Terminal Application  |  How to customize Windows Terminal Application  |  How to customize Windows Terminal Key Bindings  |  How to use Blazor EditForm for Model Validation using C#  |  How to publish a Blazor Server Application to IIS  ]

5 2 votes
Article Rating
Subscribe
Notify of
guest
7 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
7
0
Would love your thoughts, please comment.x
()
x