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

查看:64
本文介绍了如何打包面向.NET Framework和Universal Windows Platform的.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表面。

  • Offers some shared functionality to both .NET Framework 4.6 and Universal Windows Platform.
  • Offers some platform-specific functionality to each (e.g. specialized classes or APIs, including XAML user controls for UWP) with potential platform-specific dependencies on external libraries.
  • Is architecture-agnostic (AnyCPU).
  • The portable subset of which can be used by other portable libraries that target a compatible API surface.

这是一系列问答,记录了我对现代主题的发现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:

  • How to package a .NET Framework library?
  • How to package a .NET library targeting the Universal Windows Platform?
  • How to package a portable .NET library targeting .NET Core?
  • How to package a multi-architecture .NET library that targets the Universal Windows Platform?
  • How to package a .NET library that targets the Universal Windows Platform and depends on Visual Studio extension SDKs?


推荐答案

此答案基于用于打包.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的平台特定的类库。

  • 用于Universal的特定于平台的类库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="..\bin\Release\MyPortableLibrary.*" target="lib\net46" />
        <file src="..\bin\Release\MyPortableLibrary.*" target="lib\uap10.0" />
        <file src="..\bin\Release\MyPortableLibrary.*" target="lib\dotnet" />

        <file src="..\..\MyDotNetLibrary\bin\Release\MyDotNetLibrary.*" target="lib\net46" />

        <!-- Double wildcard also ensures that the subdirectory is packaged. -->
        <file src="..\..\MyUwpLibrary\bin\Release\MyUwpLibrary**" target="lib\uap10.0" />
    </files>
</package>

请注意,定义了两组独立的依赖项:一个通用组和一个特定于Universal 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软件包现在可以安装到通用的.NET Framework 4.6项目中。 Windows Platform项目和可移植库项目以兼容的API表面为目标。可移植库的功能将在所有平台上导出,并且特定平台的库也将在适当的平台上使用。

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.

记住要使用Release配置来构建解决方案在创建NuGet程序包之前。

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和Universal Windows Platform的.NET库,并包括特定于平台的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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