如何打包面向通用 Windows 平台并依赖于 Visual Studio 扩展 SDK 的 .NET 库? [英] How to package a .NET library that targets the Universal Windows Platform and depends on Visual Studio extension SDKs?

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

问题描述

如何打包依赖于 Visual Studio 扩展 SDK 的通用 Windows 平台库,例如 Microsoft Player Framework?

How do I package a Universal Windows Platform library that depends on Visual Studio extension SDKs such as the Microsoft Player Framework?

具体来说,我希望我的库的用户能够在按下 NuGet 中的安装按钮后立即使用它,而无需手动将扩展 SDK 添加到他们的项目中.当然,前提是实际安装了相应的扩展 SDK.

Specifically, I want users of my library to be able to use it immediately after pressing the Install button in NuGet, without having to manually add the extension SDKs to their projects. Assuming, of course, that the appropriate extension SDKs are actually installed.

这是一系列问题和答案,记录了我对现代 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 of .NET Framework library packaging and the principles of Universal Windows Platform library packaging. Read the linked answers first to better understand the following.

当您在项目中直接引用 Visual Studio 扩展 SDK 时,.csproj 文件中包含以下代码段:

When you directly reference a Visual Studio extension SDK in a project, the following snippet is included in the .csproj file:

<SDKReference Include="Microsoft.PlayerFramework.Xaml.UWP, Version=3.0.0.2">
  <Name>Microsoft Player Framework</Name>
</SDKReference>

NuGet 提供的功能可以在安装 NuGet 包时执行等效操作.您必须做的第一件事是在您的项目中创建一个 .targets 文件(例如 MyLibraryUsingExtensionSdk.targets),其中包含要添加到安装了您的库的项目中的相关 XML.您需要从库的 .csproj 文件中复制相关的 <SDKReference> 元素,并包含任何父元素,从而创建一个可以合并的完整 XML 文档.

NuGet offers functionality that enables an equivalent action to be performed when installing a NuGet package. The first thing you must do is to create in your project a .targets file (e.g. MyLibraryUsingExtensionSdk.targets) that contains the relevant XML to be added to the project into which your library is installed. You will want to copy the relevant <SDKReference> element from your library's .csproj file and also include any parent elements, creating a full XML document that can be merged.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <SDKReference Include="Microsoft.PlayerFramework.Xaml.UWP, Version=3.0.0.2">
            <Name>Microsoft Player Framework</Name>
        </SDKReference>
    </ItemGroup>
</Project>

将此文件的构建操作设置为无,以避免构建过程不必要地触及它.

Set the build action of this file to None, to avoid it being needlessly touched by the build process.

通过将此 .targets 文件包含在 NuGet 包结构中的适当位置,它将在运行时自动合并到使用您的库的项目中.您要实现以下包结构:

By including this .targets file in the appropriate location in the NuGet package structure, it will be automatically merged at runtime into projects that make use of your library. You want to achieve the following package structure:

+---build
|   \---uap10.0
|           MyLibraryUsingExtensionSdk.targets
|
\---lib
    \---uap10.0
        |   MyLibraryUsingExtensionSdk.dll
        |   MyLibraryUsingExtensionSdk.pdb
        |   MyLibraryUsingExtensionSdk.pri
        |   MyLibraryUsingExtensionSdk.XML
        |
        \---MyLibraryUsingExtensionSdk
                ExampleControl.xaml
                MyLibraryUsingExtensionSdk.xr.xml

您可以使用以下 .nuspec 模板创建这样的包:

You can create such a package using the following .nuspec template:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata minClientVersion="3.2">
        <id>Example.MyLibraryUsingExtensionSdk</id>
        <version>1.0.0</version>
        <authors>Firstname Lastname</authors>
        <description>Example of a simple UWP library that depends on an extension SDK.</description>
    </metadata>
    <files>
        <!-- Causes referencing this NuGet package to also automatically reference the relevant extension SDKs. -->
        <file src="MyLibraryUsingExtensionSdk.targets" target="build\uap10.0\MyLibraryUsingExtensionSdk.targets" />

        <file src="..\bin\Release\MyLibraryUsingExtensionSdk**" target="lib\uap10.0" />
    </files>
</package>

请注意,安装此类 NuGet 包后,Visual Studio 将需要重新加载解决方案才能完全识别扩展 SDK.构建将立即运行而不会出现问题,但 IntelliSense 在重新加载之前不会选择新的扩展 SDK.

Note that Visual Studio will require a solution reload to fully recognize the extension SDK after installing such a NuGet package. Builds will work immediately without problems but IntelliSense will not pick up the new extension SDK until a reload.

不幸的是,这种方法确实需要您对扩展 SDK 的版本号进行硬编码,这可能会出现问题.在撰写本文时,我不知道如何指定版本范围或与版本无关的引用.

Unfortunately, this approach does require you to hardcode the version number of the extension SDK, which can be problematic. At the moment of writing, I know of no way to specify a range of versions or a version-independent reference.

请记住在创建 NuGet 包之前使用发布配置构建您的解决方案.

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

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

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

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

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