How can I reference UWP classes in PowerShell

后端 未结 1 1994
梦谈多话
梦谈多话 2021-02-14 21:47

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 t

相关标签:
1条回答
  • 2021-02-14 22:37

    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

    0 讨论(0)
提交回复
热议问题