I have a .NET 6.0 C# class library project that contains platform-independent code (let's call this BusinessLogic
). In my same solution, I would like to create a project for a WinUI 3 app that references this class library (let's call this WindowsApp
). I would also like to create a class library specific to the Windows platform (so I can access the Windows.Storage
namespace from within that class library, for example... let's call this WindowsOS
).
I get an error when attempting to set this up. I have tried two techniques:
First technique
WindowsOS
.WindowsOS
project, add reference to BusinessLogic
. No problem.WindowsOS
project, install NuGet packages Microsoft.Windows.SDK.BuildTools
and Microsoft.WindowsAppSDK
. This gives me an error about numeric comparisons on the target platform, similar to the one described in this GitHub issue. Afterwards, the project becomes unloadable in Visual Studio.Second technique
WindowsOS
.WindowsOS
project, add reference to BusinessLogic
. This gives me an error immediately, simply refusing to allow the reference to be added.I suspect there appears to be some compatibility issue going on. I reviewed the Microsoft docs on .NET Standard versions, as well as this helpful StackOverflow question about .NET Core vs .NET Standard class libraries, and from what I can tell UWP may not be capable of referencing .NET class libraries.
My end goal is to create a WinUI 3 desktop app that references these cross-platform class libraries. My common code is contained in these libraries, and I may make an Android app or other platform app in a separate project that also references these same cross-platform class libraries. How do I do this?
EDIT: Here is a screenshot of the error from technique #1:
I figured out the answer. In the Visual Studio project properties (screenshot below), there is a Target OS
property. That property defaults to (None)
.
Given the names of the projects in the question, set the property accordingly:
Windows
in the WindowsOS
project. This will give access to Windows-platform specific namespaces (such as Windows.Storage
). WindowsOS
can still have a project reference to BusinessLogic
(and any .NET 6.0 C# library) as before.BusinessLogic
project properties.WindowsApp
(WinUI 3) project also requires no changes, and can reference BOTH the WindowsOS
project (which now has a Target OS
of Windows
) AND the BusinessLogic
project (which still has a Target OS
property of (None)
.Something to keep in mind: the WindowsApp
project and the WindowsOS
project will now both have Target OS version
and Supported OS version
properties. If you set these to different values in each project, you will get compiler warnings about a potential conflict (a user could install the app with a lower version of Windows, but that app then references the library which may require a higher version of Windows than the user has, for example). This does not matter if you are only using APIs supported in BOTH versions of Windows, but to be safe make sure these are consistent between your projects.