如何在 PowerShell 中引用 UWP 类 [英] How can I reference UWP classes in PowerShell

查看:85
本文介绍了如何在 PowerShell 中引用 UWP 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用通用 Windows 平台库中的数据类型,如何在 PowerShell 中引用包含的命名空间或程序集?

例如,我想使用 Windows.Data.Json.JsonObject 来解析我一些 json.

如果这是一个常规的 .NET 课程,我会做类似的事情

Add-Type -AssemblyName Windows.Data.Json$jsonObject = [Windows.Data.Json.JsonObject]::Parse('{data:["powershell","rocks"]}')

但是这个策略让我失望:

Add-Type :无法添加类型.无法找到程序集Windows.Data.Json".在行:1 字符:1+ 添加类型 -AssemblyName Windows.Data.Json+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : ObjectNotFound: (Windows.Data.Json:String) [Add-Type],异常+ FullQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand

现在,我假设 Windows.Data.Json 命名空间的程序集是 Windows.Data.Json.dll 可能是错误的,但是 API 参考实际上似乎没有包含任何对包含文件的引用,这让我相信 dll 文件实际上不是我应该寻找的.

我认为 UWP 有它自己的酷似 GAC 的商店,我可以从中加载共享库,我只是不知道如何.

所以基本上我的问题是,如何将 UWP 共享库加载到 PowerShell 中,我应该如何引用 UWP 类型文字?

在 Windows 10(内部版本 1703)上运行 PowerShell 5.1

解决方案

在发布这个问题后不久,我偶然发现了 BurntToast 的 GitHub 存储库,该模块允许从 PowerShell 引发 UWP Toast 通知,它引用 WinRT ToastNotificationManager 类型,如下所示:

[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]

所以,看起来我所追求的 UWP 类的语法是:

[,,ContentType = WindowsRuntime]

考虑到这一点,我用我在问题中给出的例子进行了尝试,瞧:

PS C:\>$jsonObjectClass = [Windows.Data.Json.JsonObject,Windows.Data.Json,ContentType=WindowsRuntime]PS C:\>$jsonObject = $jsonObjectClass::Parse('{"data":["powershell","rocks"]}')PS C:\>$json对象核心价值---- -----数据 ["powershell","rocks"]

在引用一次类型名称后,我似乎可以在类型字面量中使用类名称而无需对其进行限定:

[Windows.Data.Json.JsonObject]::Parse("{}") # 现在可以正常工作而不会抛出错误

仍然很想找到有关此的任何文档

I want to use a data type from a Universal Windows Platform library, how can I reference the containing namespace or assembly in PowerShell?

For example, I want to use the Windows.Data.Json.JsonObject class to parse me some json.

Had this been a regular .NET class, I would have done something like

Add-Type -AssemblyName Windows.Data.Json
$jsonObject = [Windows.Data.Json.JsonObject]::Parse('{data:["powershell","rocks"]}')

But this strategy fails me with:

Add-Type : Cannot add type. The assembly 'Windows.Data.Json' could not be found.
At line:1 char:1
+ Add-Type -AssemblyName Windows.Data.Json
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Windows.Data.Json:String) [Add-Type], Exception
    + FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand

Now, it could be that I'm simply wrong in assuming that the assembly for the Windows.Data.Json namespace is Windows.Data.Json.dll, but the API reference does not actually seem to contain any references to containing files, leading me to believe that a dll file is actually not what I should be looking for.

I assume UWP has it's own cool GAC-like store from which I can load shared libraries, I simply just don't know how.

So basically my question is, how can I load a UWP shared library into PowerShell, and how should I reference UWP type literals?

Running PowerShell 5.1 on Windows 10 (build 1703)

解决方案

Shortly after posting this question, I stumbled on the GitHub repo for BurntToast, a module that allows raising UWP Toast Notifications from PowerShell, and it references the WinRT ToastNotificationManager type like this:

[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]

So, it looks like the syntax I'm after for UWP classes is:

[<class name>,<namespace>,ContentType = WindowsRuntime]

With this in mind, I tried it with the example I gave in the question and lo and behold:

PS C:\> $jsonObjectClass = [Windows.Data.Json.JsonObject,Windows.Data.Json,ContentType=WindowsRuntime]
PS C:\> $jsonObject = $jsonObjectClass::Parse('{"data":["powershell","rocks"]}')
PS C:\> $jsonObject

Key  Value                 
---  -----                 
data ["powershell","rocks"]

After referencing the type name once, I seem to be able to use the class name in a type literal without qualifying it as well:

[Windows.Data.Json.JsonObject]::Parse("{}") # works without throwing errors now

Still very keen to find any documentation on this though

这篇关于如何在 PowerShell 中引用 UWP 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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