Netstandard库 - 在便携式类中引用

时间:2016-12-01 10:29:07

标签: .net xamarin uwp portable-class-library .net-standard

我对netstandard库的使用有疑问:

假设我有UWP项目。该项目允许在1.4及以下版本中安装netstandard库。我正在使用netstandard 1.2版创建PCL库,并添加此库作为UWP项目的参考。我正在为PCL库添加一些nuget包。这个nuget也是netstandard 1.2版本。

  1. 我可以使用UWP项目中的nuget包而不添加对它的引用吗?我已经在某个地方读过,但我无法自己动手。
  2. 如果我只使用创建的PCL库中的nuget包,那么一切都构建正常,但在运行时,事实证明PCL抛出了无法找到引用的nuget库的异常。 nuget未复制到最终bin目录,因此无法找到。怎么处理?我应该从UWP项目中引用这个库吗?
  3. 如果我向UWP项目添加nuget引用,那么实际引用的nuget可能与PCL库引用的库不同,因为nuget.org可能会为UWP项目和不同的PCL netstandard1.2库返回不同的库。所以在运行时PCL库会找到引用的dll,但这个dll不是apriopriate版本。
  4. 我错过了什么或它应该那样工作?

    所描述的问题基于通用Windows平台,但我认为问题更为普遍,而不是使用此平台。

1 个答案:

答案 0 :(得分:1)

Can I use nuget package from UWP project without adding reference to it? I have read somewhere that I can, but I can't get it working on my own.

As far as I know, it is not possible to use a Nuget package without adding reference to it. Nuget simply add a reference to these external packages in the .nuspec file. If you don't want to use nuget package, you can directly reference the libraries. You can find the libraries under C:\Users\<username>\.nuget\packages\<Destination Package>

If I use nuget package only from created PCL library then everything builds ok, but during runtime it turns out that PCL throws exception that cannot find referenced nuget library. The nuget is not copied to final bin directory, so it cannot be found. How to handle that? Should I reference this library from UWP project?

It could be caused by a variety of reasons. Could you please offer some steps to reproduce this problem?

If I add nuget reference to UWP project then the actual referenced nuget may differ from library referenced from PCL library, because nuget.org may return different libraries for UWP project and dfifferent PCL netstandard1.2 library. So during runtime PCL library will find referenced dll but this dll is not in apriopriate version.

Please update the the library in PCL to the same version.