我试图理解为什么这段代码将在Powershell ISE中运行,而不是从Powershell控制台运行。
function close-window (){
# close
$hash.window.Dispatcher.Invoke("Normal",[action]{ $hash.window.close() })
# clean up
$powershell.EndInvoke($handle) | Out-Null
$runspace.Close() | Out-Null
$powershell.Dispose() | Out-Null
}
$hash = [hashtable]::Synchronized(@{})
$runspace = [runspacefactory]::CreateRunspace()
$runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$runspace.Open()
$runspace.SessionStateProxy.SetVariable("hash",$hash)
$Powershell = [PowerShell]::Create()
$powershell.AddScript({
$xaml = [xml]@"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window" Title="Splash Screen" WindowStyle="None" WindowStartupLocation="CenterScreen"
Width="499" Height="272" BorderBrush="#FF2AA0F1" ShowInTaskbar = "False" AllowsTransparency="True"
ResizeMode = "NoResize" BorderThickness="1" >
</Window>
"@
$reader = New-Object System.Xml.XmlNodeReader $xaml
$hash.window = [Windows.Markup.XamlReader]::Load($reader)
$hash.window.Add_MouseRightButtonUp({ $hash.window.close() })
$hash.window.ShowDialog()
})
$Powershell.Runspace = $runspace
$handle = $Powershell.BeginInvoke()从ISE运行它,您将得到一个XAML窗口出现的预期结果。保存该文件并从控制台运行它,它将失败,将其复制并粘贴到控制台中,然后它就会以静默方式失败。
它包含一个带有同步哈希表的单个运行空间,因为它是XAML,所以我将运行空间作为单个线程单元启动。当从控制台运行时,尝试了MTA和STA。
目的是使用这段代码作为启动屏幕的一部分。
发布于 2015-05-26 09:29:24
脚本缺少对任何程序集名称的引用,这不是ISE中的问题,而是控制台所必需的。
Add-Type -AssemblyName PresentationCore, PresentationFrameworkhttps://stackoverflow.com/questions/30392577
复制相似问题