简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM