2

我一直在阅读有关从不同运行空间写入 UI 的博客(http://learn-powershell.net/2012/10/14/powershell-and-wpf-writing-data-to-a-ui-from-a -不同的运行空间/)。

我基本上是在尝试这样做,以便我可以单击 UI 中的按钮并运行 PowerShell 脚本并在该脚本发生时捕获该脚本的输出并更新 WPF UI 控件而不会冻结 UI。

我尝试了一个直接编写一些输出的基本示例,但它似乎挂起 UI。我正在使用运行空间和调度程序,但我似乎陷入了困境。

有任何想法吗?

谢谢。

Add-Type –assemblyName PresentationFramework
Add-Type –assemblyName PresentationCore
Add-Type –assemblyName WindowsBase


$uiHash = [hashtable]::Synchronized(@{})
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open() 
$newRunspace.SessionStateProxy.SetVariable('uiHash',$uiHash)

$psCmd = [PowerShell]::Create().AddScript({
[xml]$xaml = @"
<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Window" Title="Patcher" Height="350" Width="525" Topmost="True">
    <Grid>
        <Label Content="A Builds" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="88" RenderTransformOrigin="0.191,0.566"/>
        <ListBox HorizontalAlignment="Left" Height="269" Margin="10,41,0,0" VerticalAlignment="Top" Width="88"/>
        <Label Content="New Build" HorizontalAlignment="Left" Margin="387,10,0,0" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="387,41,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label Content="B Builds" HorizontalAlignment="Left" Margin="117,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.528,-0.672"/>
        <ListBox HorizontalAlignment="Left" Height="269" Margin="103,41,0,0" VerticalAlignment="Top" Width="88"/>
        <Label Content="C Builds" HorizontalAlignment="Left" Margin="185,10,0,0" VerticalAlignment="Top"/>
        <ListBox HorizontalAlignment="Left" Height="269" Margin="196,41,0,0" VerticalAlignment="Top" Width="88"/>
        <Button x:Name="PatchButton" Content="Patch!" HorizontalAlignment="Left" Margin="426,268,0,0" VerticalAlignment="Top" Width="75"/>
        <RichTextBox x:Name="OutputTextBox" HorizontalAlignment="Left" Height="194" Margin="289,69,0,0" VerticalAlignment="Top" Width="218">
            <FlowDocument>
                <Paragraph>
                    <Run Text=""/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>

    </Grid>
</Window>
"@

$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$uiHash.Window = [Windows.Markup.XamlReader]::Load($reader)

$uiHash.Button = $uiHash.Window.FindName("PatchButton")
$uiHash.OutputTextBox = $uiHash.Window.FindName("OutputTextBox")

$uiHash.OutputTextBox.Dispatcher.Invoke("Render", [Windows.Input.InputEventHandler]    {$uiHash.OutputTextBox.UpdateLayout()}, $null, $null)

$uiHash.Window.ShowDialog() | Out-Null
})
$psCmd.Runspace = $newRunspace
$data = $psCmd.BeginInvoke()

# The next line fails (null-valued expression)
<#
$uiHash.OutputTextBox.Dispatcher.Invoke("Normal", [action]{
    for ($i = 0; $i -lt 10000; $++) {
        $uiHash.OutputTextBox.AppendText("hi")
    }
})#>
4

4 回答 4

1

出于好奇,您是否考虑过PowerShell不是启动WPF并显示一些输出,而是应该反过来?

也许您应该从WPF应用程序中调用PowerShell,并捕获要显示的输出?

http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C

于 2014-04-18T07:08:22.090 回答
1

我遇到了同样的问题,并且看到了一些建议,这些建议要么引用 C# 类,要么引用 UI 放置在 worker 中而不是实际 worker 任务中的示例。无论如何,在花了相当长的一段时间后,我想通了:

Add-Type -AssemblyName PresentationFramework
$Xaml = New-Object System.Xml.XmlNodeReader([XML]@"
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" WindowStartupLocation = "CenterScreen" ShowInTaskbar = "True">
    <Grid>
        <TextBox x:Name = "TextBox"/>
    </Grid>
</Window>
"@)

Function Start-Worker {
    $TextBox = $Window.FindName("TextBox")
    $SyncHash = [hashtable]::Synchronized(@{Window = $Window; TextBox = $TextBox})
    $Runspace = [runspacefactory]::CreateRunspace()
    $Runspace.ThreadOptions = "ReuseThread"         
    $Runspace.Open()
    $Runspace.SessionStateProxy.SetVariable("SyncHash", $SyncHash)          
    $Worker = [PowerShell]::Create().AddScript({
        for($Progress=1; $Progress -le 10; $Progress++){
            $SyncHash.Window.Dispatcher.Invoke([action]{$SyncHash.TextBox.AppendText($Progress)})
            Start-Sleep 1                                                       # Some background work
        }
    })
    $Worker.Runspace = $Runspace
    $Worker.BeginInvoke()
}

$Window = [Windows.Markup.XamlReader]::Load($Xaml)                              # Requires -STA mode
$Window.Add_Loaded({Start-Worker})
[Void]$Window.ShowDialog()

有关 Windows 控件示例,请参阅:PowerShell:未执行表单的作业事件操作

于 2016-04-19T11:24:31.797 回答
0

我对你的代码做了一些调整。我不会在调度程序中运行您的 For 循环,而是在 For 循环中运行调度程序块。我进行了测试,它似乎并没有锁定我(有时性能不是最好的,但这是意料之中的)并一直写到窗口。

for ($i = 0; $i -lt 10000; $i++) {
    $uiHash.OutputTextBox.Dispatcher.Invoke("Normal", [action]{
        $uiHash.OutputTextBox.AppendText("hi")
    })
}
于 2014-04-18T11:13:57.487 回答
0

这可能有点晚了,但请尝试下载WPFRunspace,它为 WPF 和 Win Forms GUI 脚本提供了一个 Powershell backgroundworker cmdlet,这些脚本将在后台执行并在收到输出或完成时更新您的 GUI。

于 2016-01-06T19:25:54.020 回答