如何打包面向 .NET Framework 和通用 Windows 平台的 .NET 库并包含特定于平台的功能? [英] How to package a .NET library targeting .NET Framework and Universal Windows Platform and include platform-specific functionality?

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

问题描述

如何以现代通用方式打包具有以下属性的 .NET 库?

How do I package a .NET library that has the following properties in a modern general-purpose way?

  • 为 .NET Framework 4.6 和通用 Windows 平台提供一些共享功能.
  • 为每个平台提供一些特定于平台的功能(例如专门的类或 API,包括用于 UWP 的 XAML 用户控件),并具有对外部库的潜在平台特定依赖性.
  • 与架构无关(AnyCPU).
  • 面向兼容 API 表面的其他可移植库可以使用其中的可移植子集.

这是一系列问题和答案,记录了我对现代 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 库的原则用于打包通用 Windows 平台库的原则用于打包可移植库的原则.首先阅读链接的答案以更好地理解以下内容.

This answer builds upon the principles used to package .NET Framework libraries, the principles used to package Universal Windows Platform libraries and the principles used to package portable libraries. Read the linked answers first to better understand the following.

要为所描述的平台集提供服务,您需要将解决方案相应地构建为多个类库项目:

To serve the described set of platforms, you need to structure your solution accordingly into multiple class library projects:

  • 用于 .NET Framework 4.6 的特定于平台的类库.
  • 适用于通用 Windows 平台的特定于平台的类库.
  • 一个面向通用 API 表面的可移植库.

您需要实现以下 NuGet 包结构:

You will want to achieve the following NuGet package structure:

---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

如果您熟悉上面链接的其他答案,那么您应该对此比较熟悉.唯一的特殊部分是便携式库存在三个副本,因为其内容将提供给所有目标平台.

If you have familiarized yourself with the other answers linked above, this should seem relatively familiar to you. The only special part is that the portable library exists in three copies since its contents are to be offered for all target platforms.

使用基于以下模板的 nuspec 文件可以实现所需的包结构:

The desired package structure can be achieved by using a nuspec file based on the following template:

<?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="..inReleaseMyPortableLibrary.*" target="lib
et46" />
        <file src="..inReleaseMyPortableLibrary.*" target="libuap10.0" />
        <file src="..inReleaseMyPortableLibrary.*" target="libdotnet" />

        <file src="....MyDotNetLibraryinReleaseMyDotNetLibrary.*" target="lib
et46" />

        <!-- Double wildcard also ensures that the subdirectory is packaged. -->
        <file src="....MyUwpLibraryinReleaseMyUwpLibrary**" target="libuap10.0" />
    </files>
</package>

请注意,定义了两组独立的依赖项:一组通用组和一组特定于通用 Windows 平台,因为 UWP 库对 Newtonsoft.Json 包有额外的依赖项.您可以将相同的模式扩展到具有特定于平台的依赖项的任意数量的平台.

Note that two separate sets of dependencies are defined: one general-purpose group and one specific to Universal Windows Platform, as the UWP library has an extra dependency on the Newtonsoft.Json package. You can extend the same pattern to any number of platforms with platform-specific dependencies.

就是这样 - 此 NuGet 包现在可以安装到面向兼容 API 表面的 .NET Framework 4.6 项目、通用 Windows 平台项目和可移植库项目中.可移植库的功能将在所有平台上导出,特定平台的库也将在适当的平台上使用.

That's it - this NuGet package can now be installed into .NET Framework 4.6 projects, Universal Windows Platform projects and portable library projects that target a compatible API surface. The functionality of the portable library will be exported on all platforms, with the platform-specific libraries also used on the appropriate platforms.

记得在创建 NuGet 包之前使用 Release 配置构建您的解决方案.

Remember to build your solution using the Release configuration before creating the NuGet package.

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

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

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

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