我有以下代码。目的是在按下按钮后更改Label
的文本
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
class MyForm : System.Windows.Forms.Form {
MyForm($mystuff) {
#Do-Stuff
$this.Add_Load( $this.MyForm_Load )
}
$MyForm_Load = {
$mlabel = [System.Windows.Forms.Label]::new()
$mlabel.Name = "status"
$mlabel.Text = "enabled"
$mbutton = [System.Windows.Forms.Button]::new()
$mbutton.Text = "toggle state"
$mbutton.Location = [System.Drawing.Point]::new(100,100)
$mbutton.Add_Click( $this.mbutton_click )
$this.Controls.Add($mlabel)
$this.Controls.Add($mbutton)
}
$mbutton_click = {
if ($this.Parent.Controls["status"].Text -eq "enabled"){
$this.Parent.Controls["status"].Text = "disabled"
}
else{
$this.Parent.Controls["status"].Text = "enabled"
}
}
}
$foo = [MyForm]::new("test")
$foo.ShowDialog()
由于以下错误,我无法直接从脚本运行代码:
At C:\Users\wakatana\oop_vs_proc\forms2_oop.ps1:4 char:16
+ class MyForm : System.Windows.Forms.Form {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.Form].
At C:\Users\wakatana\oop_vs_proc\forms2_oop.ps1:11 char:20
+ $mlabel = [System.Windows.Forms.Label]::new()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.Label].
At C:\Users\wakatana\oop_vs_proc\forms2_oop.ps1:15 char:21
+ $mbutton = [System.Windows.Forms.Button]::new()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.Button].
At C:\Users\wakatana\oop_vs_proc\forms2_oop.ps1:18 char:30
+ $mbutton.Location = [System.Drawing.Point]::new(100,100)
+ ~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Drawing.Point].
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : TypeNotFound
在VS代码中也显示相同的错误,并用红色下划线标出。但是,当我启动powershell控制台并复制粘贴脚本的第一行(Add-Type -AssemblyName System.Windows.Forms
),然后从此控制台运行脚本时,脚本运行良好。有什么问题吗?