这个答案建立在用于打包面向 .NET Framework 的库的原则 https://stackoverflow.com/questions/34611829/how-to-package-a-net-framework-library。首先阅读链接的答案以更好地理解以下内容。
首先,您必须在项目构建设置中选中“生成库布局”复选框。否则,将无法使用库导出的 XAML 用户控件。确保将此设置应用于所有构建配置和架构。
除了基本的 3 个资产(请参阅上面链接的答案)之外,您还需要从构建输出目录打包以下附加资产:
- MyUwpLibrary.pri
- MyUwpLibrary 子目录
需要这些额外的资源才能使用程序集导出的 XAML 用户控件。始终包含这些资源,即使您尚未从库中导出任何 XAML - 您有一天可能会这样做,但稍后会很难记住!
要发布 UWP 库,您需要创建具有以下结构的 NuGet 包:
\---lib
\---uap10.0
| MyUwpLibrary.dll
| MyUwpLibrary.pdb
| MyUwpLibrary.pri
| MyUwpLibrary.xml
|
\---MyUwpLibrary
HelloWorld.xaml
MyUwpLibrary.xr.xml
如果您熟悉 .NET Framework 库发布,这应该看起来非常熟悉和简单。您可以将以下模板用于 nuspec 文件:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata minClientVersion="3.2">
<id>Example.MyUwpLibrary</id>
<version>1.0.0</version>
<authors>Firstname Lastname</authors>
<description>Example of a UWP library that exports a user control.</description>
<dependencies>
<dependency id="Newtonsoft.Json" version="8.0.1" />
</dependencies>
</metadata>
<files>
<!-- The double wildcard will also grab all the resource files and the resource subdirectory. -->
<file src="..\bin\Release\MyUwpLibrary**" target="lib\uap10.0" />
</files>
</package>
就是这样,真的!请记住在创建 NuGet 包之前使用发布配置构建解决方案。有关更多详细信息,请参阅上面链接的有关打包 .NET Framework 库的答案。
示例库及相关打包文件为可以在 GitHub 上找到 https://github.com/sandersaares/nuget-package-authoring。这个答案对应的解决方案是SimpleUwpLibrary。