Omortis
Omortis

Reputation: 1530

How do I add XAML windows to a .Net Core "WPF Application"?

In Visual Studio 2019, if I create a new "WPF Application" using .Net Core (top in the screenshot below) it will create a project for me that does in fact include a working XAML setup. However, there is no way that I can find to add a new XAML window through the VS interface. Right-clicking on a folder, choosing Add and then Window (WPF) just gives you the codebehind, no XAML anywhere.

If instead I create a new "WPF App (.NET Framework)" (bottom in the screenshot) It creates a similar project but I can in fact add XAML windows via right-click and selecting Add as above, including the relevant codebehind.

What gives? If we want to use .Net Core do we have to create the XAML files manually? Am I missing something? XAML is explicitly not listed under the .Net Core option, but the project creation process does indeed create XAML files in the project as expected.

enter image description here

Upvotes: 0

Views: 1907

Answers (2)

MegaMaid
MegaMaid

Reputation: 11

With suggestions above, I was able to get this working in VS2019. To condense the answer:

  1. In the project properties, ensure the "Output Type" is "Windows Application".
  2. Ensure the target framework is ".Net 5.0"
  3. Then directly edit the .csproj file by adding <UseWPF>true</UseWPF> to the first <PropertyGroup>.
  4. Right-click on the project, select: Add > Window

Upvotes: 1

Omortis
Omortis

Reputation: 1530

I seem to have answered my own question but it seems useful so I will leave it up here. When you create your .Net Core WPF Application, if you choose .NetCore 3.1 (Long-term support) you get the behavior I describe above (no ability to add XAML windows). If you choose .NET 5.0 (Current) it works as expected and you can add XAML windows.

Upvotes: 0

Related Questions