【问题标题】:How to reference WinRT components in Powershell如何在 Powershell 中引用 WinRT 组件
【发布时间】:2015-01-21 14:01:20
【问题描述】:

我正在尝试在 PowerShell 中使用 Windows.System 命名空间,它是 WinRT 的一部分。

程序集不是dll,而是winmd。将程序集加载到 PowerShell 中的两种方式似乎都不起作用

#[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral\Windows.winmd")

#Add-Type -Path "C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral\Windows.winmd"

我知道使用 Windows API 比加载我的 .NET 代码要复杂一些。我有 pinched 使用部分 win32 的代码,WinRT 解决方案是否类似?

$script:nativeMethods = @();
function Register-NativeMethod([string]$dll, [string]$methodSignature)
{
    $script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; }
}
function Add-NativeMethods()
{
    $nativeMethodsCode = $script:nativeMethods | % { "
        [DllImport(`"$($_.Dll)`")]
        public static extern $($_.Signature);
    " }

    Add-Type @"
        using System;
        using System.Runtime.InteropServices;
        public static class NativeMethods {
            $nativeMethodsCode
        }
"@
}

Register-NativeMethod "user32.dll" "int MessageBox (IntPtr hWnd, string text, string caption,int type)"
Add-NativeMethods
[NativeMethods]::MessageBox(0, "Please do not press this again.", "Attention", 0)| Out-Null

我的目标是在 PowerShell 中运行 Windows.System.Launcher.LaunchFileAsync("C:\file");。如何加载我需要的 WinRT 组件?

【问题讨论】:

    标签: windows powershell windows-8 windows-runtime


    【解决方案1】:

    要将 WinRT 二进制文件加载到 PowerShell 中,请使用以下示例:

    > [Windows.Data.Json.JsonValue,Windows.Web,ContentType=WindowsRuntime]
    > $value = new-object Windows.Data.Json.JsonObject
    > $value.Stringify()
    
    {}
    

    我想你需要一个StorageFile:

    > [Windows.System.Launcher,Windows.Web,ContentType=WindowsRuntime]
    > [Windows.System.Launcher]::LaunchFileAsync
    
    OverloadDefinitions
    -------------------
    static Windows.Foundation.IAsyncOperation[bool] LaunchFileAsync(Windows.Storage.IStorageFile file)
    static Windows.Foundation.IAsyncOperation[bool] LaunchFileAsync(Windows.Storage.IStorageFile file, Windows.System.LauncherOptions options)
    

    要创建一个,您可以这样做:

    > [Windows.Management.Core.ApplicationDataManager,Windows.Web,ContentType=WindowsRuntime]
    > $value = [Windows.Management.Core.ApplicationDataManager]::CreateForPackageFamily("BackgroundTaskApp_mxmz85hp10cp4")
    > $asyncInfo = $value.LocalFolder.CreateFileAsync("foo.txt")
    

    对于await *Async() 方法,您需要执行类似于create a wrapper 的操作。

    【讨论】:

    猜你喜欢
    • 2012-05-21
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多