此答案基于principles of .NET Framework library packaging 和principles of Universal Windows Platform library packaging。首先阅读链接的答案以更好地理解以下内容。
当您在项目中直接引用 Visual Studio 扩展 SDK 时,.csproj 文件中包含以下 sn-p:
<SDKReference Include="Microsoft.PlayerFramework.Xaml.UWP, Version=3.0.0.2">
<Name>Microsoft Player Framework</Name>
</SDKReference>
NuGet 提供的功能可以在安装 NuGet 包时执行等效操作。您必须做的第一件事是在您的项目中创建一个 .targets 文件(例如 MyLibraryUsingExtensionSdk.targets),其中包含要添加到安装您的库的项目中的相关 XML。您需要从库的 .csproj 文件中复制相关的 <SDKReference> 元素并包含所有父元素,从而创建一个可以合并的完整 XML 文档。
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<SDKReference Include="Microsoft.PlayerFramework.Xaml.UWP, Version=3.0.0.2">
<Name>Microsoft Player Framework</Name>
</SDKReference>
</ItemGroup>
</Project>
将此文件的构建操作设置为无,以避免它被构建过程不必要地触及。
通过将此 .targets 文件包含在 NuGet 包结构中的适当位置,它将在运行时自动合并到使用您的库的项目中。你想实现如下包结构:
+---build
| \---uap10.0
| MyLibraryUsingExtensionSdk.targets
|
\---lib
\---uap10.0
| MyLibraryUsingExtensionSdk.dll
| MyLibraryUsingExtensionSdk.pdb
| MyLibraryUsingExtensionSdk.pri
| MyLibraryUsingExtensionSdk.XML
|
\---MyLibraryUsingExtensionSdk
ExampleControl.xaml
MyLibraryUsingExtensionSdk.xr.xml
您可以使用以下 .nuspec 模板创建这样的包:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata minClientVersion="3.2">
<id>Example.MyLibraryUsingExtensionSdk</id>
<version>1.0.0</version>
<authors>Firstname Lastname</authors>
<description>Example of a simple UWP library that depends on an extension SDK.</description>
</metadata>
<files>
<!-- Causes referencing this NuGet package to also automatically reference the relevant extension SDKs. -->
<file src="MyLibraryUsingExtensionSdk.targets" target="build\uap10.0\MyLibraryUsingExtensionSdk.targets" />
<file src="..\bin\Release\MyLibraryUsingExtensionSdk**" target="lib\uap10.0" />
</files>
</package>
请注意,安装此类 NuGet 包后,Visual Studio 将需要重新加载解决方案才能完全识别扩展 SDK。构建将立即运行而不会出现问题,但 IntelliSense 在重新加载之前不会选择新的扩展 SDK。
不幸的是,这种方法确实需要您对扩展 SDK 的版本号进行硬编码,这可能会产生问题。在撰写本文时,我不知道如何指定版本范围或与版本无关的参考。
请记住在创建 NuGet 包之前使用发布配置构建您的解决方案。
一个示例库和相关的打包文件是available on GitHub。这个答案对应的解决方案是UwpLibraryDependingOnExtensionSdks。