找不到 XAML 个元素作为变量 - Powershell 2.0

Cannot find XAML elements as variables - Powershell 2.0

我的脚本 运行 在 Powershell V2.0 ISE 中,但在 Powershell 脚本中没有 运行。为什么是这样?我知道这里的变量不能为空,但是我从 XAML 中读取了这些变量并且单击按钮确实有效。我收到以下错误:

You cannot call a method on a null-valued expression,
At C:\Program Files (x86)\Novertz\Main Script\Novertz.ps1 char:7
+ $FEL_CP6K01_button.add.click <<<< <<
     +Category Info           :InvalidOperation

我阅读 XAML 的内容如下:

[CmdletBinding()]

Param(

[Parameter(Mandatory=$True,Position=1)]

[string]$XamlPath

)



[xml]$Global:xmlWPF = Get-Content -Path $XamlPath



#Add WPF and Windows Forms assemblies

try{

 Add-Type -AssemblyName         PresentationCore,PresentationFramework,WindowsBase,system.windows.forms

} catch {

 Throw "Failed to load Windows Presentation Framework assemblies."

}



#Create the XAML reader using a new XML node reader

$Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object     System.Xml.XmlNodeReader $xmlWPF))



#Create hooks to each named object in the XAML

$xmlWPF.SelectNodes("//*[@Name]") | %{

 Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global

 }

和 powershell 脚本:

#Required to load the XAML form and create the PowerShell Variables

cd "C:\Users\Leeds TX 12\Desktop\Novertz\GUI"

./LoadDialog.ps1 -XamlPath 'C:\Users\Leeds TX     12\Desktop\Novertz\GUI\myform.xaml'

$FEL_CP6K01_button.add_Click({#powershell script#})

$xamGUI.ShowDialog() | out-null

XAML:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta‌​tion" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="No-Vertz"  Height="1080" Width="1920"> 
<Grid Margin="0,0,-233,-101"> <Grid.Effect> <DropShadowEffect/>       </Grid.Effect> 

<Button Name="FEL_CP6K01_button" Content="FEL_CP6K_CH01"  HorizontalAlignment="Left" Margin="23,69,0,0" VerticalAlignment="Top" Width="99"  Height="59"/> 

运行 单线程单元模式下的 powershell -STA