Nuget 是 dotnet 开发中必不可少的包管理工具,但不仅仅局限于 dotnet 项目,在 VS 中使用 C++ 开发的时候,也可以使用 Nuget 来引用第三方组件。同样也可以用 Nuget 把 native 的一些组件打包,提供给自己或者别人使用。

这片博文记录一下如何把 WinRT 组件打包成 nuget 包,供 UWP 项目使用。

之前提供给合作伙伴我们的 WinRT 组件时,是直接把 .winmd文件 和 .dll文件发给他们,其中 .winmd 提供声明,.dll 提供具体的实现。然后使用者就可以手动引用和复制这两个文件到项目里面使用。

但通过 Nuget 包的形式,一方面便于管理和发布,另一方面可以很好的做到版本控制。

实战如下:

准备原材料

Demo.winmd

主要包含命名空间,类之类的声明,因为本质上使用的是 COM 技术,所以即使只添加对 .winmd 的引用,UWP 项目依旧可以成功编译。

Demo.dll

包含对应 .winmd 的具体 COM 实现。

Nuget.exe

用来打包 nuget 包,可以从 nuget 官网下载(https://www.nuget.org/downloads )

创建 Nuget 包声明文件(.nuspec)

可以使用如下命令来生成一个名为 demo.nuspec 的模板文件

nuget spec demo

在此文件中,需要加入对 Demo.winmd 和 Demo.dll 文件的引用,修改完成后如下

<?xml version="1.0"?>
<package >
<metadata>
<id>com.cq.nugetdemo</id>
<version>1.0.0.0</version>
<title>Nuget Demo</title>
<authors>cq</authors>
<owners>cq</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Just a test</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2019</copyright>
<tags>demo</tags>
</metadata>
<files>
<!-- WinMd and IntelliSense files -->
<file src=".\Nuget.winmd" target="lib\uap10.0"/> <!-- DLLs and resources -->
<file src=".\Nuget.dll" target="runtimes\win10-x64\native"/> <!-- .targets -->
<file src="com.cq.nugetdemo.targets" target="build\native"/>
</files>
</package>

metadata 节点提供一些必要的 nuget 包信息声明,最重要的是 id,这个要保证唯一性。

引用文件的位置是以 demo.nuspec 文件的位置为基准,可以使用相对路径。

需要将 nuget.winmd 文件放入 lib 文件夹下的 uap10.0 目录,其中 lib 文件夹会在最终生成的 nuget 包中创建,uap10.0 代表此文件只会被 UWP 应用引用。

需要将 demo.dll 文件放入 runtimes 文件夹下,同样这个文件夹我们不需要手动创建,它会在最终生成的 nuget 包中出现。win10-x64 代表该 dll 为 x64 cpu 架构,win10-x86 就代表32位,native 则表示此 dll的类型。

com.cq.nugetdemo.targets 文件和 demo.nuspec 文件在同一目录,它主要是为了让 C++ 的 UWP 项目也能正确添加对 nuget 包中 dll 和 winmd 文件的引用,在 C# 项目里面IDE会帮我们添加。 该文件如下:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<NugetDemo-Platform Condition="'$(Platform)' == 'Win32'">x86</NugetDemo-Platform>
<NugetDemo-Platform Condition="'$(Platform)' != 'Win32'">$(Platform)</NugetDemo-Platform>
</PropertyGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'UAP'">
<Reference Include="$(MSBuildThisFileDirectory)..\..\lib\uap10.0\Nuget.winmd">
<Implementation>Demo.dll</Implementation>
</Reference>
<ReferenceCopyLocalPaths Include="$(MSBuildThisFileDirectory)..\..\runtimes\win10-$(NugetDemo-Platform)\native\Demo.dll" />
</ItemGroup>
</Project>

虽然 C# 的 UWP 项目不需要 .targets 文件,但是为了让 IDE 能正确添加对 DLL 文件的引用,我们需要保持 dll 和 winmd 文件同名,否则在运行时会抛出异常。在编译时,会有如下警告:

warning APPX1707: No implementation file was provided for the .winmd file 'C:\Users\...\.nuget\packages\com.cq.nugetdemo\1.0.0.3\lib\uap10.0\demo.winmd'. To generate registration information in the app manifest, specify the 'Implementation' metadata on the .winmd reference item in the project file.

创建 nuget 包(.nupkg)

nuget.exe pack NugetDemo.nuspec -Version 1.0.0.3

在创建的时候,可以指定版本号,这方便我们用自动化脚本来生成 nuget 包。执行此命令后,在demo.nuspec 同级目录下会生成对应的 .nupkg。

使用 nuget 包

正常使用 nuget 包时,可以通过 VS 里的 nuget 包管理器去安装。但是如果我们的 nuget 包只想本地使用,那我们可以给 nuget 包管理器添加一个本地的包源,

在 .sln 同级目录,创建一个 Nuget.config 文件,然后可以在里面配置本地的 nuget source

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="MyLocalSource" value=".\NugetPackage" />
</packageSources>
</configuration>

其中 MyLocalSource 是包源的名称,.\NugetPackage 是包源的路径,也是相对路径

现在就可以在包管理器中选择对应的本地源,然后安装生成的 .nupkg

参考

我主要参考了微软的官方文档:

https://docs.microsoft.com/en-us/nuget/guides/create-uwp-packages

微软文档会更加全面一些,而这篇博客主要是我的具体实践,对比该文档去掉了一些 暂时没发现有什么用 的步骤,添加了一些值得注意的点。

微软文档里提到的如下两点,在这个 demo 里没有用到:

Package Resource Index (PRI) file

微软文档

Every app package should contain a binary index of the resources in the app. This index is created at build time and it is contained in one or more Package Resource Index (PRI) files.

  • A PRI file contains actual string resources, and an indexed set of file paths that refer to various files in the package.
  • A package typically contains a single PRI file per language, named resources.pri.
  • The resources.pri file at the root of each package is automatically loaded when the ResourceManager is instantiated.
  • PRI files can be created and dumped with the tool MakePRI.exe.
  • For typical app development you won't need MakePRI.exe because it's already integrated into the Visual Studio compile workflow. And Visual Studio supports editing PRI files in a dedicated UI. However, your localizers and the tools they use might rely upon MakePRI.exe.
  • Each PRI file contains a named collection of resources, referred to as a resource map. When a PRI file from a package is loaded, the resource map name is verified to match the package identity name.
  • PRI files contain only data, so they don't use the portable executable (PE) format. They are specifically designed to be data-only as the resource format for Windows. They replace resources contained within DLLs in the Win32 app model.
  • The size limit on a PRI file is 64 kilobytes.

简而言之,PRI 文件相当于传统 Win32 开发中的 资源 DLL 文件。

XML documentation (Demo.xml)

在 IDE 的 C++ output file 属性页面,可以打开如下选项

Generate XML Documentation files (/doc)

打开之后,就会生成和我们的 native dll 同名的 .xml 文件,大致如下:

<?xml version="1.0"?>
<doc>
<assembly>
<name>ClassLibrary</name>
</assembly>
<members>
<member name="T:ClassLibrary.Demo">
<summary>
A demo how to use documentation.
</summary>
</member>
<member name="M:ClassLibrary.Demo.GetDate">
<summary>
Get date of now.
</summary>
<returns>a date time struct</returns>
</member>
</members>
</doc>

当把这个 .xml 和 .dll 同事分发给使用者时,使用者在 VS IDE 里面就能获得对代码注释的 智能感知 能力。当然这要求我们在提供编写 .dll 的源代码的时候,提供规定格式的良好的注释。

注释格式可以参考如下:

C++ https://docs.microsoft.com/en-us/cpp/build/reference/xml-documentation-visual-cpp?view=vs-2019

Dotnet(C#) https://docs.microsoft.com/en-us/dotnet/csharp/codedoc

One more thing

事实上在 demo.dll 是一个 COM 服务器,使用前需要先注册,在 UWP 项目里如何注册这个 COM 服务器呢?

当我们添加对 .winmd 和 .dll 的引用后,在 UWP 的 AppMenifest 文件里面会自动添加如下节点:

<Package>
<Extensions>
<Extension Category="windows.activatableClass.inProcessServer">
<InProcessServer>
<Path>Nuget.dll</Path>
<ActivatableClass ActivatableClassId="Nuget.Class" ThreadingModel="both" />
</InProcessServer>
</Extension>
</Extensions>
</Package>

Github

Demo 下载

欢迎关注我的微信公众号

[UWP] 为WinRT组件创建Nuget包的更多相关文章

  1. 如何使用跨平台工具创建 NuGet 包(转)

    原文地址 https://docs.microsoft.com/zh-cn/dotnet/articles/core/deploying/creating-nuget-packages 内容 对于 . ...

  2. 创建NuGet包

    创建NuGet包 在创建一个NuGet包之前我们应该先创建一个以.nuspec为后缀的xml清单文件,这个清单文件描述了包的内容,在安装NuGet包的过程中这个清单文件扮演者很重要的角色.实际上它的作 ...

  3. 项目文件中的已知 NuGet 属性(使用这些属性,创建 NuGet 包就可以不需要 nuspec 文件啦)

    知道了 csproj 文件中的一些常用 NuGet 属性,创建 NuGet 包时就可以充分发挥新 Sdk 自动生成 NuGet 包的优势,不需要 nuspec 文件啦.(毕竟 nuspec 文件没有 ...

  4. warning MSB3162: 所选的“Microsoft Report Viewer 2012 Runtime”项需要“Microsoft.SqlServer.SQLSysClrTypes.11.0”。在“系统必备”对话框中选择缺少的系统必备组件,或者为缺少的系统必备组件创建引导程序包。

    warning MSB3162: 所选的“Microsoft Report Viewer 2012 Runtime”项需要“Microsoft.SqlServer.SQLSysClrTypes.11. ...

  5. 一个技术汪的开源梦 —— 基于 .Net Core 的组件 Nuget 包制作 & 发布

    一个技术汪的开源梦 —— 目录 微软的 ASP.Net Core 强化了 Nuget 的使用,所有的 .Net Core 组件均有 Nuget 管理,所以有必要探讨一下 .Net Core 组件制作 ...

  6. 自动将 NuGet 包的引用方式从 packages.config 升级为 PackageReference

    在前段时间我写了一篇迁移 csproj 格式的博客 将 WPF.UWP 以及其他各种类型的旧样式的 csproj 文件迁移成新样式的 csproj 文件,不过全过程是手工进行的,而且到最后处理 XAM ...

  7. 使用命令行打包 nuget 包

    对于那些不打算涉及这么复杂而又想制作自己的 nuget 包的园友们,我是推荐使用 Nuget Package Explorer 来制作的.关于这个图形化的 nuget 包管理软件的使用,博客园内有相关 ...

  8. 使用NuGet发布自己的.NET NuGet 包( .NET Standard & Windows)

    发布自己的nuget包 STEP 1:获取API Key 首先,你需要到NuGet上注册一个新的账号,然后在My Account页面,获取一个API Key,或者是自建服务器上生成一个API Key( ...

  9. 使用GUI工具高效构建你自己的Nuget包

    写这篇文章的原因是我在学习构建nuget包的时候,发现了一个官方推荐的GUI工具,而官方的工具介绍文章已经过时,一些地方和现在最新版本的工具有些差异,所以特意利用假期最后一个下午写下来,希望能帮助更多 ...

随机推荐

  1. JavaSE----03.数据类型

    03.数据类型 1.数据类型分类     Java是强类型语言,Java中的数据类型分为两大类,分别是基本数据类型和引用数据类型.其中,基本数据类型由Java语言定义,其数据占用内存的大小固定,在内存 ...

  2. 一致性哈希(PHP核心技术与最佳实践)

    <?php /** * 分布式缓存部署方案 * 当有1台cache服务器不能满足我们的需求,我们需要布置多台来做分布式服务器,但是 * 有个问题,怎么确定一个数据应该保存到哪台服务器上呢? * ...

  3. redis安装详细

      1.cd命令到你的下载文件夹下 2.输入命令下载redis ,可以自己选择下载的路径,redis表示版本号 wget http://download.redis.io/releases/redis ...

  4. 05-04 scikit-learn库之主成分分析

    目录 scikit-learn库之主成分分析 一.PCA 1.1 使用场景 1.2 代码 1.3 参数 1.4 属性 1.5 方法 二.KernelPCA 三.IncrementalPCA 四.Spa ...

  5. 用go语言爬取珍爱网 | 第三回

    前两节我们获取到了城市的URL和城市名,今天我们来解析用户信息. 用go语言爬取珍爱网 | 第一回 用go语言爬取珍爱网 | 第二回 爬虫的算法: 我们要提取返回体中的城市列表,需要用到城市列表解析器 ...

  6. PMP(第六版)中的控制账户、规划包、工作包

    PMP(第六版)中的控制账户.规划包.工作包 控制账户是一个管理控制点,在该控制点上,把范围.预算和进度加以整合,并与挣值比较,以测量绩效.控制账户拥有2个或以上的工作包,但每个工作包只与一个控制账户 ...

  7. [NOIp2009] luogu P1072 Hankson 的趣味题

    把 c 改成 d 下了两个点. 题目描述 已知正整数 a0,a1,b0,b1a_0,a_1,b_0,b_1a0​,a1​,b0​,b1​,设某未知正整数 xxx 满足: xxx 和 a0a_0a0​ ...

  8. [JZOJ100047] 【NOIP2017提高A组模拟7.14】基因变异

    Description 21 世纪是生物学的世纪,以遗传与进化为代表的现代生物理论越来越多的 进入了我们的视野. 如同大家所熟知的,基因是遗传因子,它记录了生命的基本构造和性能. 因此生物进化与基因的 ...

  9. 自力更生Collections.sort发现比较结果混乱?Comparator的锅还是强转类型导致?

    近日开发任务时间充裕一些,于是有时间回顾一下项目. 我关注到了项目中使用的七牛云的对象存储服务. 作为测试需要上传了一些图片,但七牛的控制台却无法将内容按照上传时间排序或者是按照日期查询,由于buck ...

  10. win10系统plsql卡顿、菜单闪烁解决办法

    右键快捷方式--属性--兼容性: 设置为以win7模式运行,以管理员模式运行.如图: