gpt4 book ai didi

wpf - 如何调用在 Powershell 中另一个运行空间中运行的 WPF 表单元素的方法

转载 作者:行者123 更新时间:2023-12-01 04:45:09 25 4
gpt4 key购买 nike

我正在尝试在 PowerShell 中使用运行空间来增强小型 GUI 应用程序的性能。

到目前为止,我得到的是一个有两个用途的运行空间:首先,它保存我所有的其他运行空间,其次,它解析我的 GUI xml 文件并显示它。
它还将节点添加到同步哈希表中,因此我可以在所有运行空间中访问它。正如您可以想象的那样,我在这个 GUI 中有一些按钮,单击时会触发操作;非常简单的东西,实际上到目前为止它工作得很好。我可以在运行空间之间交换数据,还可以在单​​击某个按钮时更新 GUI。

但是,我无法在我的 xml 文件中的空堆栈面板上调用 addChild() 方法。我基本上想要做的只是将复选框添加到堆栈面板(名为“CheckboxViewer”)。

$checkbox = New-Object System.Windows.Controls.CheckBox
$checkbox.Content = "Hello world"
$syncHash.checkbox = $checkbox
$syncHash.CheckboxViewer.Dispatcher.Invoke(
[Action]{
#this is working:
$syncHash.CheckboxViewer.Background = 'Black'
#this is not working
$syncHash.CheckboxViewer.AddChild($syncHash.checkbox)
}, "Normal")

我收到的错误消息是尝试直接访问另一个运行空间(不使用调度程序)时收到的典型错误消息:

Exception calling "AddChild" with "1" argument(s): "The calling thread cannot access this object because a different thread owns it.



任何想法我在这里做错了什么?任何形式的帮助将不胜感激,谢谢!

最佳答案

试试这个:

$syncHash.Window.Dispatcher.Invoke([action]{
$checkbox = New-Object System.Windows.Controls.CheckBox
$checkbox.Content = "Hello world"
$syncHash.CheckboxViewer.AddChild($checkbox)
}, "Normal")

要添加控件,您必须在 [action] 中声明它。

关于wpf - 如何调用在 Powershell 中另一个运行空间中运行的 WPF 表单元素的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47225223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com