how to make UWP class library to support WindowsAppSdk apps - c#

I have developed a UWP class library , and it is already used in various projects.
Now I want to make this UWP class library to support WindowsAppSdk apps too.
When I try to refer UWP Library in WindowsAppSdk app, getting error like "Project is not compatible".
My UWP class library min supported version - Windows 10 fall creators update (16299).
Should I recreate my UWP Library as WindowsAppSdk library ?
Can anyone help me understand this
Thanks
Noorul.

Should I recreate my UWP Library as WindowsAppSdk library ?
No, but you should convert it into a .NET Standard library that can be consumed by both UWP and WinUI (.NET) applications, and any other type of application whose runtime implementation supports the version of the .NET Standard specification that you choose to target.
UWP apps cannot consume librararies that are targeted against a specific platform such as .NET and vice versa. That's where .NET Standard comes in. It enables you to share code across multiple .NET implementations and platforms.

Related

Use UWP library in .NET Framework / .NET Standard application

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.

Referencing .Net class library running under .Net 4.5.x in a UWP app

I am currently working on a UWP app (which automatically has reference to Microsoft.NETCore.UniversalWindowsPlatform when I added it). Now I want to reference a class library (contains the models for code reuse) running under .Net Framework 4.5.x into the UWP app but when I try to add the dll it throws this error
" could not be added. The project targets '.NETCore' while file
reference targets '.NETFramework'. This is not a supported scenario."
Is there any work around for this?
Any answers/tips are highly appreciated!
Because as the error says UWP doesn't support .Net libraries.
Possible solution is to extract and port needed code so you can use it in .Net and UWP projects.
There are several options: .Net Standart or Portable class library

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

System.Xml.Linq.XElement does not have Load method when building UWP

I created a new UWP app and wanted to load an XML file using the xelement class.
While looking on mdsn, I see I can do this using the load(string) method
https://msdn.microsoft.com/en-us/library/system.xml.linq.xelement(v=vs.110).aspx
In my UWP project the xelement is missing the Load method.
Why is this happening ?
Is it because I am not actually using the .NET while building UWP apps ? Or I am using a subset of .NET while building UWP apps ?
While looking on msdn how can I verify if a method from a .NET class is also available while developing a UWP app ?
Is it because I am not actually using the .NET while building UWP apps ? Or I am using a subset of .NET while building UWP apps ?
The .Net APIs that can be used in UWP apps is called .NET for UWP apps. .NET for UWP apps provides a set of managed types that you can use to create Universal Windows Platform apps for Windows 10 using C# or Visual Basic. Note that .NET for UWP apps includes a subset of the types provided in the full .NET Framework for each namespace.
While looking on msdn how can I verify if a method from a .NET class is also available while developing a UWP app ?
As #Paweł Hemperek said, usually, we can find if a .NET Framework class is available in UWP apps by checking the Version Information in the bottom of the document. If we can find Universal Windows Platform under Version Information then we should be able to use this class in UWP apps.
But please note that one class can be used in UWP not means that all of its properties and methods are available in UWP. For example, XmlElement Class is available in UWP, but XmlNode.SelectSingleNode Method (String) can't be used in UWP apps.

Load a not SIlverlight dll into a Silverlight project type

I hope isn't a stupid question...
Is possible to load a no silverlight assembly into a silverlight project without using a COM object?
Thanks in advance.
You can use the Portable Class Library (PCL)
Use this project to write and build portable assemblies that work
without modification on multiple platforms, such as Windows 7, Windows
8, Silverlight, Windows Phone, and Xbox 360. For example, you can
create classes that contain shared business logic for desktop apps,
Windows Store apps, and mobile apps, and you can then reference those
classes from your apps.
The Portable Class Library project supports a subset of assemblies
from the .NET Framework, Silverlight, .NET for Windows Store apps,
Windows Phone, and Xbox 360, and provides a Visual Studio template
that you can use to build assemblies that run without modification on
these platforms. If you don't use a Portable Class Library project,
you must target a single app type, and then manually rework the class
library for other app types. With the Portable Class Library project,
you can reduce the time and costs of developing and testing code by
building portable assemblies that are shared across apps for different
devices.
Also, Microsoft's Scott Hanselman has written a few articles about the PCL, this one should be a good starting point to find the resources you need to get started: Cross-Platform Portable Class Libraries with .NET are Happening

Categories