[英]Powershell with GUI will not set variable
I am learning Powershell and attempting to create a GUI interface like the example here . 我正在学习Powershell并尝试创建一个类似于此处示例的GUI界面。
However, when I run this as it is, $x
is never defined. 但是,当我按原样运行时,
$x
永远不会被定义。 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({Write-Host "worked";$objForm.Close()})
It does return worked
. 它确实恢复了
worked
。
Your actionblock is running in a seperate local scope. 您的actionblock在单独的本地范围内运行。 To modify a global(session-wide) variable, use a scope-modifier(
$global:varname
). 要修改全局(会话范围)变量,请使用scope-modifier(
$global:varname
)。 Eg: 例如:
$OKButton.Add_Click({$global:x=$objListBox.SelectedItem;$objForm.Close()})
More about scopes at Technet - about_Scopes (or write Get-Help about_scopes
in PowerShell) 有关Technet范围的更多信息- about_Scopes (或在PowerShell中编写
Get-Help about_scopes
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.