0

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

  1. Create a .NET 6.0 C# class library WindowsOS.
  2. In WindowsOS project, add reference to BusinessLogic. No problem.
  3. In 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

  1. Create a Class Library (Universal Windows) project WindowsOS.
  2. In 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:

enter image description here

2
  • 1
    It wants to parse the SDK version number. So use as indicated in the github issue (say) "net6.0-windows10.0.19041.0" Commented Oct 2, 2022 at 22:31
  • @HansPassant you are correct on how to fix the warning! I explain how to do this in my answer - but investigating the project properties XML (.csproj) I did see that changing the properties as described in my answer does indeed adjust this value as you describe! Thanks! Commented Oct 9, 2022 at 7:58

1 Answer 1

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:

  1. Set the property value to 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.
  2. No change necessary to the BusinessLogic project properties.
  3. The 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.

Screenshot of Visual Studio project properties page

Not the answer you're looking for? Browse other questions tagged or ask your own question.