2015-01-21 1 views
1

Я пытаюсь использовать пространство имен Windows.System, входящее в состав WinRT, в PowerShell.Как ссылаться на компоненты WinRT в Powershell

Вместо 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 

Моя цель - запустить Windows.System.Launcher.LaunchFileAsync("C:\file"); в PowerShell. Как я могу загрузить компоненты WinRT, которые мне нужны?

ответ

2

Чтобы загрузить 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.