在powershell中运行wpf程序的一种方法,修正
在run.sp1中
# Create a runspace to run Hello World
$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs.ApartmentState, $rs.ThreadOptions = “STA”, “ReuseThread”
$rs.Open()
$sb = [System.Management.Automation.ScriptBlock]::Create((Join-Path $pwd "hell0.ps1"))
$psCmd = $sb.GetPowerShell()
$psCmd.Runspace = $rs
$null = $psCmd.BeginInvoke()
在hell0.ps1中
# Reference the WPF assemblies
Add-Type –assemblyName PresentationFramework
Add-Type –assemblyName PresentationCore
Add-Type –assemblyName WindowsBase
$window = New-Object Windows.Window
$window.Title = $window.Content = “Hello World. Check out PowerShell and WPF Together.”
$window.SizeToContent = “WidthAndHeight”
$null = $window.ShowDialog()
# Create a runspace to run Hello World
$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs.ApartmentState, $rs.ThreadOptions = “STA”, “ReuseThread”
$rs.Open()
$sb = [System.Management.Automation.ScriptBlock]::Create((Join-Path $pwd "hell0.ps1"))
$psCmd = $sb.GetPowerShell()
$psCmd.Runspace = $rs
$null = $psCmd.BeginInvoke()
在hell0.ps1中
# Reference the WPF assemblies
Add-Type –assemblyName PresentationFramework
Add-Type –assemblyName PresentationCore
Add-Type –assemblyName WindowsBase
$window = New-Object Windows.Window
$window.Title = $window.Content = “Hello World. Check out PowerShell and WPF Together.”
$window.SizeToContent = “WidthAndHeight”
$null = $window.ShowDialog()
![](http://img.zemanta.com/pixy.gif?x-id=9bd9037b-9b02-80fe-afcb-8f558d47f680)