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
- Create a .NET 6.0 C# class library
WindowsOS. - In
WindowsOSproject, add reference toBusinessLogic. No problem. - In
WindowsOSproject, install NuGet packagesMicrosoft.Windows.SDK.BuildToolsandMicrosoft.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
- Create a Class Library (Universal Windows) project
WindowsOS. - In
WindowsOSproject, add reference toBusinessLogic. 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 OSproperty. That property defaults to(None).Given the names of the projects in the question, set the property accordingly:
Windowsin theWindowsOSproject. This will give access to Windows-platform specific namespaces (such asWindows.Storage).WindowsOScan still have a project reference toBusinessLogic(and any .NET 6.0 C# library) as before.BusinessLogicproject properties.WindowsApp(WinUI 3) project also requires no changes, and can reference BOTH theWindowsOSproject (which now has aTarget OSofWindows) AND theBusinessLogicproject (which still has aTarget OSproperty of(None).Something to keep in mind: the
WindowsAppproject and theWindowsOSproject will now both haveTarget OS versionandSupported OS versionproperties. 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.