wpf.net-corewpf-core

How to reference System.Windows namespace in .Net Core?


I'm a long time WPF (.Net Framework) developer finally dipping my toes into .Net Core for the first time. I'm currently trying to convert a small v4.7.2 WPF application to .Net Core 3.1. The solution contains a WPF app project and a class library project, with only a handful of classes each, so I decided it would be easier to create a new solution containing a WPF app and class library project (both .Net Core 3.1), then copy the cs and xaml files from the original solution. However I seem to be falling at the first hurdle...

Many of the classes use types from the System.Windows namespace (and System.Windows.Media, System.Windows.Controls, etc). These resolve fine in the WPF app project but not the class library. I've noticed the WPF app project has a reference to the framework "Microsoft.WindowsDesktop.App.Wpf", but the class library project doesn't, which I assume is my problem. However I can't for the life of me figure out how to add this framework into the project. How do I do it?!

I've seen solutions elsewhere of adding <UseWPF>true</UseWPF> to the class lib csproj file, but this has no effect.


Solution

  • As usual, no sooner do I post the question than I find the answer. I also had to change the SDK in the csproj file to:

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">