Use UWP library in .NET Framework / .NET Standard application - c#

Is there a way to use a C# library written in the UWP framework in a .NET Framework (or .NET Standard) application?
I try to get a Bluetooth (BLE) module working for a .NET Framework application in Windows 11. As far as I know, the native Bluetooth support is only possible using UWP, so I can't switch here. The .NET Framework application is also fixed, it contains external libraries from suppliers that can't be updated to .NET Standard or .NET 6. However, I can write an adapter class that uses .NET Standard 2.0, so I can work with it if a solution for .NET Standard exists.
When I try to reference a UWP class from .NET Framework or .NET Standard, I always get an error like this:
"Project BLE_Module is not compatible with netstandard2.0(.NETStandard,Version=v2.0). Project BLE_Module supports: uap.10.0.17763(UAP,Version=v10.0.17763)"
I know I can reference .NET Standard classes from UWP, but that does not help me, as the UWP module is just an execution module for Bluetooth requests and does not include any business logic.
What can I do to resolve the problem? I could write the BLE_Module as a stand-alone service and interact with the service from the main program during runtime, but before I embark on this journey, I would like to know if there are any possibilities to make it work without such a specific solution (that also reduces testability and increases bug chances).

Is there a way to use a C# library written in the UWP framework in a .NET Framework (or .NET Standard) application?
Short answer: No.
That's why .NET Standard was introduced in the first place, i.e. to be able to share common code between different .NET implementations.
A class library that targets a specific platform, such as UWP, cannot be consumed from an app that targets another platform, such as for example the .NET Framework.

Native Bluetooth support does not require UWP. The Bluetooth libraries are written for the WinRT API, so you can use them from C++ or .NET Framework desktop applications.
See https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/desktop-to-uwp-enhance for more info.
If you however have an additional layer written in UWP on top of the native Bluetooth libraries, then I'm not sure if that can be consumed in a normal .NET Framework application.

Related

Use UWP methods from .Net Standard library

I know there is a way to use UWP methods from WPF apps.
My question is whether there is a way to use those methods from a .Net Standard library. Just adding the references mentioned here doesn't help. It doesn't recognize them.
Individual Platforms (.NET Framework, .NET Core, UWP, Mono) can reference .NET Standard libraries. But .NET Standard libraries cannot reference other libraries built specifically for a platform (.NET Framework, UWP etc).
Which makes sense since .NET Standard libraries themselves are supposed to be platform agnostic, so you shouldn't be able to add a library that would only work on a single platform.

Xamarin with .Net Framework

I have started to learn Xamarin. From what I read, .Net Standard allows a developer to use code from another .Net platform into Xamarin.
The examples that I read only talk about MVC or .Net Core. The documentation says that the developer can use .Net Standard with .Net Framework 4.6.1. Visual Studio 2019 has the option to include an Asp.Net Core Web API Project as part of a mobile backend.
Is there there a .Net Framework option available?
I have a lot of .Net Framework Pages that I would like to include into Xamarin.
Do I need to rewrite all the code or is there a conversion process I need to do?
.NET Standard defines the common subset of libraries and features that are shared by both .NET Core and .NET Framework. If your existing code is using .NET Framework, it's possible that most or all of the framework features that the code relies on are supported by .NET Standard. If so, it will be easy to port the code to a .NET Standard assembly, which can be used by Xamarin. The difficulty of the port will depend on how many .NET Framework-specific features you're using.
Microsoft provides a tool to analyze how compatible your code is, you can find details here

Xamarin .net standard effect on APK size

When you use a .net standard lib from the full .net framework you end up having to include/deploy .net standard versions of all the System.* assemblies the .net standard library uses.
Now that Xamarin supports .net standard and microsoft is recommending people use .net standard rather than PCL we will soon need to switch (library developers are abandoning PCL support).
My question is does using a .net standard library in a Xamarin for Android project mean that multiple system assemblies will be deployed (the .net standard version and the mono version)?
Xamarin apps are already much bigger than native and having to include duplicates of all the system assemblies we need could be a deal breaker going forward.
The .NET Standard(s) define the API surface of each version. The implementation of the API surface lies with the platform.
In the case of Xamarin the surface is implemented in the Mono framework which is deployed with every app. (And has been before)
If you reference nothing but the NETStandard Library the final .apk file will not increase.
Additional NuGet packages might bring in extra NetStandard specific dependencies (which could previously have been handled by the .net / mono framework itself)

Can UWP run in .NET?

When I create new UWP project it automatically references .NET Core. Is it possible to run it on .NET? Why it needs .NET Core? I can't find any valuable information about this...
It needs .NET Core because it is built on .NET Core, much like WPF is built on the .NET framework.
So no, you cannot change it to run on the standard .NET framework any more than you can make WPF run on .NET Core.
The .NET Core Framework is a new version of .NET for modern device and cloud workloads. It’s a general-purpose and modular implementation of the Microsoft .NET Framework that can be ported and used in many different environments for a variety of workloads.
and No, you cannot run UWP project on .Net
althousg if you install previous version of VS then you can run it on .NET

Can I use PCL library in standard .Net application?

I have PCL Library and I want to add it to standard (.net 4.6) C# console application. Everything is fine as long as I don't use any PCL specific classes inside the library. And if I do, I get an error "unsupported PCL profile". This error is not googlable. But the same library works fine in UWP application. I am searching for a solution or official explanation why I can't use PCL in non UWP application.
Yes you can. PCL is basically intersection of available API's across different platforms. The disadvantage is that the more target platforms you choose the smaller is the intersection:
Another disadvantage of PCL is that it generates separate assembly for each platform.
That's why Microsoft comes with .NET Standard - a replacement of PCL that uses different approach.
Think about .NET Standard as an interface, that defines set of API's. Then the platforms like .NET Framework, .NET Core, Xamarin.iOS, Xamarin.Android will implement the .NET Standard.
interface NetStandard1_0 {
}
interface NetStandard1_1 : NetStandard1_0{
}
interface NetStandard1_2 : NetStandard1_1{
}
net46: NetStandard1_6 {
}
dnxcode46: NetStandard1_6 {
}
As a result, you won't target specific platforms, but a version of .NET Standard instead. When your library targets .NET Standard, it can be used in any platform that implements the .NET Standards. Another advantage is you wont need separate assemblies for different platforms anymore. There will be single assembly that runs just everywhere.
However, I recommend you to wait until April 2017 when .NET Standrard 2.0 should be released. Microsoft promised that all platforms (.NET Framework, .NET Core, Xamarin.iOS, Xamarin.Android) will support this version of .NET Standard and it will have official support in Visual Studio. Also, Visual Studio projects using project.json will be converted to .csproj, so all Visual Studio projects will use the same format and it will solve a lot of compatibility issues. Cleanning of the mess that appeared in .NET last years was absolutelly necessary
Sure you can.
Just add .NET 4.6 to selected platforms:
It's appears at time when you create PCL.
More information here:
Cross-Platform Development with the Portable Class Library
Or you can change platforms in existing PCL. Just go to properties page and you will see:
Here is a good blog post about how to call UWP API from desktop App:
How to call UWP APIs from a desktop VB/C# app

Categories