简体   繁体   English

我可以在标准.Net应用程序中使用PCL库吗?

[英]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. 我有PCL库,我想将其添加到标准(.net 4.6)C#控制台应用程序中。 Everything is fine as long as I don't use any PCL specific classes inside the library. 只要我在库中不使用任何PCL特定类,一切都很好。 And if I do, I get an error "unsupported PCL profile". 如果这样做,我会收到错误消息“不受支持的PCL配置文件”。 This error is not googlable. 此错误不可查询。 But the same library works fine in UWP application. 但是相同的库在UWP应用程序中可以正常工作。 I am searching for a solution or official explanation why I can't use PCL in non UWP application. 我正在寻找解决方案或官方解释,为什么我不能在非UWP应用程序中使用PCL。

Yes you can. 是的你可以。 PCL is basically intersection of available API's across different platforms. PCL基本上是跨不同平台的可用API的交集。 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. PCL的另一个缺点是,它为每个平台生成单独的组件。

That's why Microsoft comes with .NET Standard - a replacement of PCL that uses different approach. 这就是Microsoft随附.NET Standard的原因-.NET Standard是使用不同方法的PCL的替代品。

Think about .NET Standard as an interface, that defines set of API's. 考虑将.NET Standard作为定义API集的接口。 Then the platforms like .NET Framework, .NET Core, Xamarin.iOS, Xamarin.Android will implement the .NET Standard. 然后,.NET Framework,.NET Core,Xamarin.iOS,Xamarin.Android等平台将实现.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. 因此,您将不针对特定平台,而要针对.NET Standard版本。 When your library targets .NET Standard, it can be used in any platform that implements the .NET Standards. 当您的库针对.NET Standard时,可以在实现.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. 但是,我建议您等到2017年4月发布.NET Standrard 2.0时。 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. 微软承诺所有平台(.NET Framework,.NET Core,Xamarin.iOS,Xamarin.Android)将支持此版本的.NET Standard,并将在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. 此外,使用project.json的Visual Studio项目将转换为.csproj,因此所有Visual Studio项目将使用相同的格式,并且将解决许多兼容性问题。 Cleanning of the mess that appeared in .NET last years was absolutelly necessary 绝对有必要清理.NET中去年出现的混乱情况

Sure you can. 你当然可以。 Just add .NET 4.6 to selected platforms: 只需将.NET 4.6添加到选定的平台:

在此处输入图片说明

It's appears at time when you create PCL. 创建PCL时会出现它。
More information here: Cross-Platform Development with the Portable Class Library 此处的更多信息: 使用可移植类库进行跨平台开发

Or you can change platforms in existing PCL. 或者,您可以更改现有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: 这是一篇不错的博客文章,介绍如何从桌面应用程序调用UWP API:
How to call UWP APIs from a desktop VB/C# app 如何从桌面VB / C#应用程序调用UWP API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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