首页 文章

如何打包一个针对通用Windows平台并依赖于Visual Studio扩展SDK的.NET库?

提问于
浏览
8

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

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

这是一系列问题和答案,记录了我对现代NuGet包创作主题的研究结果,特别关注NuGet 3引入的更改 . 您可能还对一些相关问题感兴趣:如何打包.NET Framework库?如何打包面向通用Windows平台的.NET库?如何打包一个面向.NET Core的可移植.NET库?如何打包面向.NET Framework和通用Windows平台的.NET库并包含特定于平台的功能?如何打包一个针对通用Windows平台的多架构.NET库?

1 回答

  • 6

    这个答案 Build 在principles of .NET Framework library packagingprinciples 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 . 您需要从库的.csproj文件中复制相关的 <SDKReference> 元素,并且还要包含任何父元素,从而创建可以合并的完整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="build\uap10.0\MyLibraryUsingExtensionSdk.targets" />
    
            <file src="..\bin\Release\MyLibraryUsingExtensionSdk**" target="lib\uap10.0" />
        </files>
    </package>
    

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

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

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

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

相关问题