PowerShell和GUI将不设置变量 [英] Powershell with GUI will not set variable
问题描述
我学习PowerShell和试图建立类似的例子这里GUI界面
I am learning Powershell and attempting to create a GUI interface like the example here.
然而,当我运行这个,因为它是 $ X
从未定义。任何想法,我可能会做错了什么?我知道它是与
However, when I run this as it is, $x
is never defined. Any idea what I could be doing wrong? I know it has something to do with:
$ OKButton.Add_Click({$ X = $ objListBox.SelectedItem; $ objForm.Close()})
因为如果我将其更改为:
Because if I change it to:
$ OKButton.Add_Click({写主机工作; $ objForm.Close()})
它返回工作
。
推荐答案
您actionblock是在一个单独的局部范围内运行。要修改全局(会话范围)变量,使用范围的改性剂( $全球:VARNAME
)。例如:
Your actionblock is running in a seperate local scope. To modify a global(session-wide) variable, use a scope-modifier($global:varname
). E.g.:
$OKButton.Add_Click({$global:x=$objListBox.SelectedItem;$objForm.Close()})
更多关于的Technet范围 - 参阅about_Scopes (或写获取 - 帮助参阅about_Scopes
在PowerShell中)
More about scopes at Technet - about_Scopes (or write Get-Help about_scopes
in PowerShell)
这篇关于PowerShell和GUI将不设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!