加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

nuget – 如何打包一个面向通用Windows平台并依赖于Visual Studi

发布时间:2020-12-14 04:06:35 所属栏目:Windows 来源:网络整理
导读:如何打包依赖于Visual Studio扩展SDK(如 Microsoft Player Framework)的通用Windows平台库? 具体来说,我希望我的库的用户能够在按下NuGet中的Install按钮后立即使用它,而无需手动将扩展SDK添加到他们的项目中.当然,假设实际安装了适当的扩展SDK. This is a
如何打包依赖于Visual Studio扩展SDK(如 Microsoft Player Framework)的通用Windows平台库?

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

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:

  • 07001
  • 07002
  • 07003
  • 07004
  • 07005
这个答案建立在 principles of .NET Framework library packaging和 principles of Universal Windows Platform library packaging之上.首先阅读链接的答案,以便更好地理解以下内容.

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

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

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

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

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

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

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

您可以使用以下.nuspec模板创建此类包:

<?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="builduap10.0MyLibraryUsingExtensionSdk.targets" />

        <file src="..binReleaseMyLibraryUsingExtensionSdk**" target="libuap10.0" />
    </files>
</package>

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

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

在创建NuGet包之前,请记住使用Release配置构建解决方案.

样本库和相关的打包文件是available on GitHub.对应于此答案的解决方案是UwpLibraryDependingOnExtensionSdks.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读