此答案基于principles used to package .NET Framework libraries、principles used to package Universal Windows Platform libraries 和principles used to package portable libraries。首先阅读链接的答案以更好地理解以下内容。
要服务于所描述的平台集,您需要将解决方案相应地构建为多个类库项目:
- .NET Framework 4.6 的特定于平台的类库。
- 通用 Windows 平台的特定于平台的类库。
- 面向通用 API 表面的可移植库。
您将希望实现以下 NuGet 包结构:
\---lib
+---dotnet
| MyPortableLibrary.dll
| MyPortableLibrary.pdb
| MyPortableLibrary.XML
|
+---net46
| MyDotNetLibrary.dll
| MyDotNetLibrary.pdb
| MyDotNetLibrary.XML
| MyPortableLibrary.dll
| MyPortableLibrary.pdb
| MyPortableLibrary.XML
|
\---uap10.0
| MyPortableLibrary.dll
| MyPortableLibrary.pdb
| MyPortableLibrary.XML
| MyUwpLibrary.dll
| MyUwpLibrary.pdb
| MyUwpLibrary.pri
| MyUwpLibrary.XML
|
\---MyUwpLibrary
HashControl.xaml
MyUwpLibrary.xr.xml
如果您已经熟悉了上面链接的其他答案,那么您应该对此比较熟悉。唯一特殊的部分是可移植库存在三个副本,因为它的内容将提供给所有目标平台。
可以通过使用基于以下模板的nuspec文件来实现所需的包结构:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata minClientVersion="3.2">
<id>Example.MyMultiSurfaceLibrary</id>
<version>1.0.0</version>
<authors>Firstname Lastname</authors>
<description>Example of a multi-platform library that exposes different API surfaces to .NET 4.6 and UWP and also includes a portable component.</description>
<dependencies>
<!-- UWP has more dependencies than other platforms (Newtonsoft.Json). -->
<group targetFramework="uap10.0">
<dependency id="Newtonsoft.Json" version="8.0.1" />
<dependency id="System.Linq" version="4.0.0" />
<dependency id="System.Numerics.Vectors" version="4.1.0" />
<dependency id="System.Resources.ResourceManager" version="4.0.0" />
<dependency id="System.Runtime" version="4.0.20" />
</group>
<!-- All other platforms - just the dependencies of the portable library here. -->
<group>
<dependency id="System.Linq" version="4.0.0" />
<dependency id="System.Numerics.Vectors" version="4.1.0" />
<dependency id="System.Resources.ResourceManager" version="4.0.0" />
<dependency id="System.Runtime" version="4.0.20" />
</group>
</dependencies>
</metadata>
<files>
<file src="..\bin\Release\MyPortableLibrary.*" target="lib\net46" />
<file src="..\bin\Release\MyPortableLibrary.*" target="lib\uap10.0" />
<file src="..\bin\Release\MyPortableLibrary.*" target="lib\dotnet" />
<file src="..\..\MyDotNetLibrary\bin\Release\MyDotNetLibrary.*" target="lib\net46" />
<!-- Double wildcard also ensures that the subdirectory is packaged. -->
<file src="..\..\MyUwpLibrary\bin\Release\MyUwpLibrary**" target="lib\uap10.0" />
</files>
</package>
请注意,定义了两组独立的依赖项:一组通用组和一组特定于通用 Windows 平台,因为 UWP 库对 Newtonsoft.Json 包有额外的依赖项。您可以将相同的模式扩展到具有特定于平台的依赖项的任意数量的平台。
就是这样 - 现在可以将这个 NuGet 包安装到 .NET Framework 4.6 项目、通用 Windows 平台项目和面向兼容 API 表面的可移植库项目中。可移植库的功能将在所有平台上导出,特定平台的库也会在适当的平台上使用。
请记住在创建 NuGet 包之前使用发布配置构建您的解决方案。
一个示例库和相关的打包文件是available on GitHub。这个答案对应的解决方案是MultiSurfaceLibrary。