如何打包面向通用 Windows 平台的 .NET 库? [英] How to package a .NET library targeting the Universal Windows Platform?

查看:21
本文介绍了如何打包面向通用 Windows 平台的 .NET 库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以现代通用方式打包通用 Windows 平台库以通过 NuGet 发布?假设我有一个用 C# 编写的 AnyCPU 程序集,它导出一些代码和 XAML 用户控件.

How do I package a Universal Windows Platform library in a modern general-purpose way for publishing via NuGet? Let's assume I have a single AnyCPU assembly written in C# that exports some code and XAML user controls.

这是一系列问题和答案,记录了我对现代 NuGet 包创作主题的发现,特别关注 NuGet 3 引入的更改.您可能还对一些相关问题感兴趣:

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:

推荐答案

这个答案建立在 用于打包面向 .NET Framework 的库的原则.首先阅读链接的答案以更好地理解以下内容.

This answer builds upon the principles used to package libraries targeting the .NET Framework. Read the linked answer first to better understand the following.

首先,您必须选中项目构建设置中的生成库布局"复选框.否则,将无法使用您的库导出的 XAML 用户控件.确保将此设置应用于所有构建配置和架构.

First you must check the "Generate library layout" checkbox in the project build settings. Without this, it will not be possible to use the XAML user controls exported by your library. Make sure to apply this setting for all build configurations and architectures.

除了基本的 3 个资产(参见上面链接的答案),您还需要从构建输出目录中打包以下附加资产:

In addition to the basic 3 assets (see answer linked above), you will also need to package the following additional assets from your build output directory:

  • MyUwpLibrary.pri
  • MyUwpLibrary 子目录

需要这些额外资源才能使用程序集导出的 XAML 用户控件.始终包含这些资产,即使您尚未从库中导出任何 XAML - 有一天您可能会这样做,但以后很难记住!

These extra resources are required to make use of XAML user controls exported by your assembly. Always include these assets, even if you do not yet export any XAML from your library - you might do it one day and this will be tricky to remember later on!

要发布 UWP 库,您需要创建一个具有以下结构的 NuGet 包:

To publish the UWP library, you need to create a NuGet package with the following structure:

---lib
    ---uap10.0
        |   MyUwpLibrary.dll
        |   MyUwpLibrary.pdb
        |   MyUwpLibrary.pri
        |   MyUwpLibrary.xml
        |
        ---MyUwpLibrary
                HelloWorld.xaml
                MyUwpLibrary.xr.xml

如果您熟悉 .NET Framework 库发布,这应该看起来非常熟悉和简单.您可以为您的 nuspec 文件使用以下模板:

If you are familiar with .NET Framework library publishing, this should look quite familiar and straightforward. You can use the following template for your nuspec file:

<?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="..inReleaseMyUwpLibrary**" target="libuap10.0" />
    </files>
</package>

就是这样,真的!请记住在创建 NuGet 包之前使用发布配置构建您的解决方案.有关更多详细信息,请参阅上面链接的有关打包 .NET Framework 库的答案.

That's it, really! Remember to build your solution using the Release configuration before creating the NuGet package. For more details, refer to the answer about packaging a .NET Framework library, linked above.

一个示例库和相关的打包文件在 GitHub 上提供.这个答案对应的解决方案是SimpleUwpLibrary.

A sample library and the relevant packaging files are available on GitHub. The solution corresponding to this answer is SimpleUwpLibrary.

这篇关于如何打包面向通用 Windows 平台的 .NET 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆