如何打包面向通用 Windows 平台的 .NET 库?
How to package a .NET library targeting the Universal Windows Platform?
如何以现代通用方式打包通用 Windows 平台库以通过 NuGet 发布?假设我有一个用 C# 编写的 AnyCPU 程序集,它导出一些代码和 XAML 用户控件。
This is a series of questions and answers that document my findings on the topic of modern NuGet package authoring, focusing especially on the changes introduced with NuGet 3. You may also be interested in some related questions:
这个答案建立在 的基础上。请先阅读链接的答案以更好地理解以下内容。
首先,您必须选中项目构建设置中的 "Generate library layout" 复选框。否则,将无法使用您的库导出的 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 包之前使用 Release 配置构建您的解决方案。有关详细信息,请参阅上面链接的有关打包 .NET Framework 库的答案。
示例库和相关打包文件为available on GitHub。这个答案对应的解决方案是SimpleUwpLibrary.
如何以现代通用方式打包通用 Windows 平台库以通过 NuGet 发布?假设我有一个用 C# 编写的 AnyCPU 程序集,它导出一些代码和 XAML 用户控件。
This is a series of questions and answers that document my findings on the topic of modern NuGet package authoring, focusing especially on the changes introduced with NuGet 3. You may also be interested in some related questions:
这个答案建立在
首先,您必须选中项目构建设置中的 "Generate library layout" 复选框。否则,将无法使用您的库导出的 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 包之前使用 Release 配置构建您的解决方案。有关详细信息,请参阅上面链接的有关打包 .NET Framework 库的答案。
示例库和相关打包文件为available on GitHub。这个答案对应的解决方案是SimpleUwpLibrary.