Unable to add a reference to project - UWP to Class Library project? - c#

A WPF project and a UWP project share the same solution. Can both projects reference a common project?
"Common" - .NET 6 target framework, class library
"UWP" - targets Windows 10 Creators Update (Build 15063)
"WPF App" - .NET 6 target framework, references "WPF2"
"WPF2" - .NET 6 target framework
The "WPF" project can reference "Common" no problem, but the "UWP" project says "Unable to add a reference to project 'Common'".
Annoyingly, the error message does not give a reason why.
How to use a class from one C# project with another C# project
Common controls for UWP and WPF
Reference a class library from UWP and ASP.NET 5 (solution here)

UWP is not compatible with .NET Core or .NET 6.
If you want to share code between a WPF app and a UWP app, your shared project should target .NET Standard 2.0.
Please refer to the docs for more information about .NET Standard and the supported .NET implementations for each version.

Related

Can I refer from a UWP project to a .NET Standard project?

I created an UWP app, and a .NET 6.0 project, and added reference of UWP project to the .NET 6 project.
Output of executing proj.bat file that runs this solution executes the following error:.
Error NU1201: Project MyUWPProject is not compatible with net6.0 (.NETCoreApp,Version=v6.0). Project MyUWPProject supports
: uap10.0.18362 (UAP,Version=v10.0.18362)
How can fix this? Is what I'm trying to do even possible?
The error message shows that the environment of your .NET 6.0 project is .net6.0 and the environment of the UWP project doesn't support .net6.0. So the UWP project is not compatible with the .NET project.
If you want to use Windows Runtime APIs, you could just make some confirugation in for your .NET 6.0 porject so that you could directly call Windows Runtime APIs in your app. You will need to Edit Your Project File and Modify TargetFramework Element to net6.0-windows10.0.19041.0.
You could check this document - Call Windows Runtime APIs in desktop apps for detailed steps.

WPF Control Library Target .NET Core 3 or .NET Framework?

I'm porting my WPF application from .NET Framework to .NET Core 3. I got this generic UI library that is published on NuGet, that is currently targeting .NET Framework v4.6.1. Now, if I just change the target to .NET Core 3, then it won't work anymore in .NET Framework projects. .NET Standard is designed to solve this kind of problem but I don't think it will help in the case of WPF projects.
What's the right thing to do here? Publish and manage 2 separate packages?
Then this library has 2 other derived libraries. If I create 2 separate packages for the base library, that means I'd have to publish and manage 2 of each derived libraries as well. This solution stinks. Got anything better?
A WPF application that targets .NET Core 3 may actually reference and consume a control library or NuGet package that targets .NET Framework 4.6.1.
You shouldn't have to do anything with the control library itself for this to work, assuming you don't use any APIs that is not present in .NET Core because then you will eventually get runtime errors.
Try to add a reference to the DLL or install the NuGet package as usual. It should work even if the NuGet package only contains a single assembly in lib\net461.

.NET windows forms project and UWP project that both reference .NET Standard class lib gets:

I added both apps (framework 4.6.1) to same solution
added a .NET Standard 2.0 class library
referenced both apps to the class library.
...and get:
ERROR...
Error NU1201 Project .NET Standard VB ClassLibrary1 is not compatible with uap10.0.15063 (UAP,Version=v10.0.15063). Project .NET Standard VB ClassLibrary1 supports: netstandard2.0 (.NETStandard,Version=v2.0) UWP App1 C:\PRIMARY\WORK\3 WindowsApp3\UWP App1\UWP App1.csproj 1
But the apps Target framework drop down only allows .NET Framework x.x.x
and the .NET Standard class library Target framework drop down only allows .NET Standard x.x
so I can't make frameworks match.
(This is on Visual Studio 2017 Pro on Windows 10 Pro.)
All of the errors...
Here are the properties of the 2 app projects and the class lib project...
You need to set the Min Version of your UWP project to 16299 (or higher). Build 16299 was the first release that supports NET Standard 2.0. Earlier versions of UWP are not compatible as the error message points out.

Reference UWP library from .net standard class library

I have a solution with some projects:
client app - UWP project
device sdk - UWP project (because of bluetooth APIs)
client app - ASP.Net Core 1.1 targeting .Net Core 2.0
client app plugins
some projects with shared model and interface classes.
I would like to use .net standard projects as much as possible (for the shared projects and preferably for the plugins) because they are more lightweight and can be edited without unloading them in Visual Studio for example.
Referencing .net standard 1.4 libraries FROM an UWP project goes fine, but referencing an UWP project FROM a .net standard 1.4 project results in an error:
Project DeviceSDK is not compatible with netstandard1.4 (.NETStandard,Version=v1.4).
Project DeviceSDK supports: uap10.0.15063 (UAP,Version=v10.0.15063)
Is this is there any workaround for this?
That's simply impossible. A cross platform library won't be able to reference a platform specific one.
I refactored the interfaces and data models of the device SDK out of that project and create a .net standard DeviceSdk.Core project. This project can then be referenced from application logic projects and the DeviceSdk main project.
So the structure becomes roughly:
ClientApp - UWP project, referencing all projects but the ClientApp.Frontend project
DeviceSdk - UWP project (because of bluetooth APIs), referencing DeviceSdk.Core
DeviceSdk.Core - .net standard 1.4
ClientApp.Frontend - ASP.Net Core 1.1 targeting .Net Core 2.0, referencing ClientApp.Core
ClientApp.Plugins - .net standard 1.4, referencing ClientApp.Core and DeviceSdk.Core
ClientApp.Core (some projects with shared model and interface classes) - .net standard 1.4
.net standard class library can't reference UWP libs. That would break the whole concept of .net Standard.
The idea is that a class library that targets specific .net standard version can only reference limited set of APIs defined by the standard.
For example, .net standard v1.4 only allows API-s listed within this file:
https://github.com/dotnet/standard/blob/master/docs/versions/netstandard1.4_ref.md
This list is frozen and is supposed to remain intact.

What kind of class library works with both Universal and Winform projects?

In visual Studio 2015, in my solution, I have a Logic tier (class library) project that is added to the references of a WinForm project.
No, I am going to add a new Windows Universal project and added the same logic tier (class library) into it.
However, visual studio doesn't allow me to add the class library as a reference to the universal app, I get an error message.
The question is, What kind of class library that can be added to the references of Universal project as well as to the Winform projects withen the same solution?
Update: the message is "Enable to add a reference to project 'project-name' ."
You need to use the Class Library (Portable) choice for the class library project:
Once you add that, you'll have to pick your target platforms:
To be able to use it with WinForms, select the .NET framework matching the target framework you have selected in your WinForms project. If your WinForms targets .NET 4.5, make sure you select that; if you changed it to target .NET 4.6, you are able to select that as the target for your portable library, and so on. Otherwise, you'll get an error saying:
Unable to add a reference to project 'Your library name'. The current
project's target is not one of or compatible with the targets of
Portable Library project 'Your library name'.
Note that you can change these options later, by going into project properties, under Library tab.

Categories

Resources