自定义函数中不会发生Powershell事件Add_Checked、Add_Unchecked

问题描述 投票:0回答:1

由于我需要在 WPF 窗口中多次创建此模式,因此我创建了如下函数。

function Group_WPF {
    param (
        [Windows.Controls.Canvas]$canvas,
        [int]$top,
        [ref]$f1,
        [string]$n1,
        [ref]$f2,
        [string]$n2,
        [ref]$isR,
        [ref]$isB,
        [ref]$bu,
    )

    $f1.Value = New-Object Windows.Controls.TextBox
    $f1.Value.Width = 380
    [Windows.Controls.Canvas]::SetLeft($f1.Value, 10)
    [Windows.Controls.Canvas]::SetTop($f1.Value, $top + 25)
    $canvas.Children.Add($f1.Value)

    $f1.Value.Add_MouseDoubleClick({
        param ($source, $e)
        $source.SelectAll()
    })

    $f2.Value = New-Object Windows.Controls.TextBox
    $f2.Value.Width = 380
    [Windows.Controls.Canvas]::SetRight($f2.Value, 10)
    [Windows.Controls.Canvas]::SetTop($f2.Value, $top + 25)
    $canvas.Children.Add($f2.Value)

    $f2.Value.Add_MouseDoubleClick({
        param ($source, $e)
        $source.SelectAll()
    })

    $isR.Value = New-Object Windows.Controls.CheckBox
    $isR.Value.Content = "Re"
    $isR.Value.IsChecked = $true
    [Windows.Controls.Canvas]::SetLeft($isR.Value, 210)
    [Windows.Controls.Canvas]::SetTop($isR.Value, $top + 50)
    $canvas.Children.Add($isR.Value)

    $isB.Value = New-Object Windows.Controls.CheckBox
    $isB.Value.Content = "Bu"
    [Windows.Controls.Canvas]::SetLeft($isB.Value, 290)
    [Windows.Controls.Canvas]::SetTop($isB.Value, $top + 50)
    $canvas.Children.Add($isB.Value)

    $bu.Value = New-Object Windows.Controls.TextBox
    $bu.Value.Width = 380
    $bu.Value.IsReadOnly = $true
    $bu.Value.Background = [System.Windows.Media.Brushes]::Gainsboro
    [Windows.Controls.Canvas]::SetRight($bu.Value, 10)
    [Windows.Controls.Canvas]::SetTop($bu.Value, $top + 50)
    $canvas.Children.Add($bu.Value)

    $bu.Value.Add_MouseDoubleClick({
        param ($source, $e)
        $source.SelectAll()
    })

    $isB.Value.Add_Checked({
        $bu.Value.IsReadOnly = $false
        $bu.Value.Background = [System.Windows.Media.Brushes]::White
        $isR.Value.IsChecked = $false
    })

    $isB.Value.Add_Unchecked({
        $bu.Value.IsReadOnly = $true
        $bu.Value.Background = [System.Windows.Media.Brushes]::Gainsboro
    })

    $isR.Value.Add_Checked({
        $isB.Value.IsChecked = $false
    })
}

所有工作,除了 Add_Checked 和 Add_Unchecked 事件。 当我尝试它们时,我收到以下错误:

The property 'IsReadOnly' cannot be found on this object. Verify that the property exists and can be set.
At line:111 char:9
+         $bu.Value.IsReadOnly = $false

The property 'Background' cannot be found on this object. Verify that the property exists and can be set.
At line:112 char:9
+         $bu.Value.Background = [System.Windows.Media.Brushes]::White

The property 'IsChecked' cannot be found on this object. Verify that the property exists and can be set.
At line:113 char:9
+         $isR.Value.IsChecked = $false

请原谅我的愚蠢。请指导我如何解决这个问题。衷心感谢您。

wpf powershell function
1个回答
0
投票

因为您需要来自调用者上下文的局部变量(即:

$bu
$isR
$isB
),您需要调用GetNewClosure

$isB.Value.Add_Checked({
    $bu.Value.IsReadOnly = $false
    $bu.Value.Background = [System.Windows.Media.Brushes]::White
    $isR.Value.IsChecked = $false
}.GetNewClosure())

$isB.Value.Add_Unchecked({
    $bu.Value.IsReadOnly = $true
    $bu.Value.Background = [System.Windows.Media.Brushes]::Gainsboro
}.GetNewClosure())

$isR.Value.Add_Checked({
    $isB.Value.IsChecked = $false
}.GetNewClosure())
© www.soinside.com 2019 - 2024. All rights reserved.