如何使用SelectNodes在XAML中找到节点?

时间:2017-03-26 08:50:45

标签: xaml powershell

我希望在以下PowerShell脚本中找到XAML中的Button节点。但我无法找到它。

[xml]$xaml1 = @' 
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="640">
    <Button Content="Button" HorizontalAlignment="Left" Margin="99,20,0,0" VerticalAlignment="Top" Width="60" Height="30"/>
</Window>
'@
$xaml1.SelectNodes("//Button")  | ForEach {
    $_.Name
}
# No OUTOUT from above code!

当我删除第3行和第4行时,它可以正常工作。

[xml]$xaml2 = @' 
<Window
        Title="MainWindow" Height="400" Width="640">
        <Button Content="Button" HorizontalAlignment="Left" Margin="99,20,0,0" VerticalAlignment="Top" Width="60" Height="30"/>
</Window>
'@
$xaml2.SelectNodes("//Button")  | ForEach {
    $_.Name
}
# OUTPUT of above code:
Button

如何使用SelectNodes在XAML中找到节点?

4 个答案:

答案 0 :(得分:0)

对不起,以下是代码:

[xml]$xaml1 = @' 
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="MainWindow" Height="400" Width="640">
        <Button Content="Button" HorizontalAlignment="Left" Margin="99,20,0,0" VerticalAlignment="Top" Width="60" Height="30"/>
</Window>
'@
[System.Xml.XmlNamespaceManager] $nsmgr = $xaml1.NameTable;
$nsmgr.AddNamespace($null, 'http://schemas.microsoft.com/winfx/2006/xaml/presentation');
$xaml1.SelectSingleNode("//Button", $nsmgr)

答案 1 :(得分:0)

试试这个:

$xaml1.Window.Button

答案 2 :(得分:0)

我通过检查每个节点的属性找到了解决方案。以下代码工作正常。

$xaml.SelectNodes("//*") | ? {$_.LocalName -eq "Button"} | % {
   # action for each Button Node
}

以下是使用上述示例代码。

[xml]$xaml = @' 
<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="300">
    <StackPanel>
        <Button x:Name="button0" Content="Button0"/>
        <Button x:Name="button1" Content="Button1"/>
        <Button x:Name="button2" Content="Button2"/>
        <Button x:Name="button3" Content="Button3"/>
        <Button x:Name="button4" Content="Button4"/>
        <Button x:Name="button5" Content="Button5"/>
        <ScrollViewer x:Name="scrollViewer" Height="150">
            <TextBlock x:Name="msg" TextWrapping="Wrap"/>
        </ScrollViewer>
    </StackPanel>
</Window>
'@

$reader = New-Object System.Xml.XmlNodeReader $xaml
$frm = [System.Windows.Markup.XamlReader]::Load($reader)

$scrollViewer = $frm.FindName("scrollViewer")
$msg = $frm.FindName("msg")
$xaml.SelectNodes("//*") | ? {$_.LocalName -eq "Button"} | % {
    $button = $frm.FindName($_.Name)
    $button.add_Click({
        $msg.Inlines.add($this.Content + " pushed`n")
        $scrollViewer.ScrollToBottom()
    })
}

$result = $frm.ShowDialog()

答案 3 :(得分:0)

$ xaml.SelectNodes(&#34; // * [@ Name]&#34;)| ForEach-Object {Set-Variable -Name($ .Name)-Value $ Form.FindName($ .Name)}

现在您将控件变为可变...

如果按钮的名称是&#34;按钮&#34;你在Powershell中有一个变量$按钮。 像这样添加一个点击然后...... $ button.add_click({write-host&#34; clicked !!&#34;})