wpf - 按钮单击以调用运行同步哈希/运行空间以在 Powershell 中刷新 WPF Listview 的函数
问题描述
我想在每次使用按钮单击事件调用的函数内的同步哈希表和运行空间添加新项目时刷新列表视图。
如果我将同步哈希/运行空间放在按钮单击中,它可以工作,但我需要使用一个函数,因为我将大量使用此代码。一段时间以来,我一直在努力解决这个问题。任何帮助将不胜感激。下面我评论了在按钮单击中工作的相同代码。
[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,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,76,0,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()
解决方案
推荐阅读
- c# - 如何在 Unity 中向左滑动按钮?
- autodesk-forge - 当我们上传 .rvt 文件时,我们总是收到错误消息“TranslationWorker-InternalFailure”。文件没有损坏,我们已经检查过了
- python - Matplot Numpy ValueError:使用序列设置数组元素
- android - 如何使用 Coroutines android 执行任务
- facebook - 使用 Graph API 获取 Facebook 主页帖子
- raspberry-pi3 - Yocto,Meta-selinux 不适用于树莓派 3
- r - 将回归结果保存在乳胶和文本中
- python - 有没有Django默认认证数据库模型的实体关系图?
- c - 计算 C 中的 searies 的几何平均值
- php - 如何在 Laravel 5.5 的自定义验证中传递数组