我想每次使用按钮单击事件中调用的函数中的同步哈希表和运行空间添加新项时刷新列表视图。
如果我将同步哈希/运行空间放在按钮单击内,它可以工作,但是我需要使用一个函数,因为我将大量使用此代码。我一直在想办法解决这个问题。任何帮助将不胜感激。在下面,我评论了按钮单击内的相同代码。
[Void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView Name="ListView" HorizontalAlignment="Left" Height="131" Margin="49,76,0" VerticalAlignment="Top" Width="289">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
<Button Name="Button" Content="AddToListView" HorizontalAlignment="Left" Margin="354,0" VerticalAlignment="Top" Width="109"/>
</Grid>
</Window>
'@
$XamlReader = New-Object system.xml.XmlNodeReader $XAML
$GUI = [Windows.Markup.XamlReader]::Load($XamlReader)
$XAML.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $GUI.findname($_.Name)}
Function AddToListView {
[CmdletBinding()]
Param (
[Parameter(Position=0,Mandatory=$true)]$ListView,[Parameter(Position=1,Mandatory=$true)]$Text,[Parameter(Position=2,Mandatory=$true)]$Color
)
$ListViewItem = New-Object System.Windows.Controls.ListViewItem
$ListViewItem.Content = $Text
$ListViewItem.Foreground = $Color
$global:SyncHash = [hashtable]::Synchronized(@{GUI=$GUI;ListView = $ListView;ListViewItem = $ListViewItem})
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable("SyncHash",$SyncHash)
$Worker = [PowerShell]::Create().AddScript({
$SyncHash.GUI.Dispatcher.Invoke([action]{
$SyncHash.ListView.Items.Add($SyncHash.ListViewItem) },"Normal"
)
})
$Worker.Runspace = $Runspace
$Worker.BeginInvoke()
}
<#$Button.Add_Click({
$global:SyncHash = [hashtable]::Synchronized(@{ GUI=$GUI;ListView = $ListView})
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable("SyncHash",$SyncHash)
$Worker = [PowerShell]::Create().AddScript({
$SyncHash.GUI.Dispatcher.Invoke([action]{
$ListViewItem = New-Object System.Windows.Controls.ListViewItem
$ListViewItem.Content = "test1"
$ListViewItem.Foreground = "green"
$SyncHash.ListView.Items.Add($ListViewItem) },"Normal"
)
Start-Sleep -Seconds 2
$SyncHash.GUI.Dispatcher.Invoke([action]{
$ListViewItem = New-Object System.Windows.Controls.ListViewItem
$ListViewItem.Content = "test2"
$ListViewItem.Foreground = "red"
$SyncHash.ListView.Items.Add($ListViewItem) },"Normal"
)
})
$Worker.Runspace = $Runspace
$Worker.BeginInvoke()
})#>
$Button.Add_Click({
AddToListView -ListView $ListView -Text "hello world!" -Color "Black"
Start-Sleep -Seconds 2
AddToListView -ListView $ListView -Text "hello world!" -Color "Black"
})
$GUI.ShowDialog()