gpt4 book ai didi

nuget - 如何打包面向通用 Windows 平台并依赖于 Visual Studio 扩展 SDK 的 .NET 库?

转载 作者:行者123 更新时间:2023-12-04 04:16:33 24 4
gpt4 key购买 nike

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

具体来说,我希望我的库的用户能够在按下 NuGet 中的安装按钮后立即使用它,而无需手动将扩展 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:

最佳答案

此答案基于 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。您需要复制相关的 <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>

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

通过将此 .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。

关于nuget - 如何打包面向通用 Windows 平台并依赖于 Visual Studio 扩展 SDK 的 .NET 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34612015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com