如何使用SelectNodes在XAML中查找节点?
我想在下面的PowerShell脚本中找到XAML中的按钮节点。但是我找不到它如何使用SelectNodes在XAML中查找节点?,xaml,powershell,Xaml,Powershell,我想在下面的PowerShell脚本中找到XAML中的按钮节点。但是我找不到它 [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">
[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!
[xml]$xaml1=@'
'@
$xaml1.SelectNodes(//按钮)| ForEach{
$\名称
}
#没有从上面的代码输出!
当我删除第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
[xml]$xaml2=@'
'@
$xaml2.SelectNodes(//按钮)| ForEach{
$\名称
}
#上述代码的输出:
按钮
如何使用SelectNodes在XAML中查找节点?很抱歉,下面是代码:
[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)
[xml]$xaml1=@'
'@
[System.Xml.XmlNamespaceManager]$nsmgr=$xaml1.NameTable;
$nsmgr.AddNamespace($null,'http://schemas.microsoft.com/winfx/2006/xaml/presentation');
$xaml1.SelectSingleNode(“//按钮“,$nsmgr)
试试这个:
$xaml1.Window.Button
我通过检查每个节点的属性找到了解决方案
$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()
[xml]$xaml=@'
'@
$reader=New Object System.Xml.XmlNodeReader$xaml
$frm=[System.Windows.Markup.XamlReader]::加载($reader)
$scrollViewer=$frm.FindName(“scrollViewer”)
$msg=$frm.FindName(“msg”)
$xaml.SelectNodes(“//*”)|?{$\.LocalName-eq“按钮”}|%{
$button=$frm.FindName($\ux.Name)
$button.add\u单击({
$msg.Inlines.add($this.Content+“push`n”)
$scrollViewer.ScrollToBottom()
})
}
$result=$frm.ShowDialog()
$xaml.SelectNodes(“//*[@Name]”)ForEach对象{Set Variable-Name($.Name)-Value$Form.FindName($.Name)}
现在您将控件设置为变量
如果按钮的名称为“Button”,则Powershell中有一个变量$Button。
添加一个像这样的点击,然后。。。
$button.add_click({write host“clicked!!“})指定名称空间参数。谢谢你的评论。根据你的反馈,我试着跟进。但不幸的是,它不起作用。[System.Xml.XmlNamespaceManager]$nsmgr=xaml1.NameTable$nsmgr.AddNamespace($null,);$xaml1.SelectSingleNode(“//按钮“,$nsmgr)FWIW,如果您添加非$null前缀(例如,
x
)并在选择器中使用它,它会起作用。请等待其他真正了解此内容的人。感谢您的努力/我尝试了前缀“x”如“x:Button”“这是有效的。但是,不幸的是,使用XAML显示GUI是不可用的。我会等别人的,谢谢。它起作用了。但我想做的是在XAML中找到许多按钮。从介绍“很抱歉,以下是代码”(sic)中不清楚这是问题的解决方案还是问题的细化。你能澄清一下吗?请记住,如果在提问后需要更改问题,可以编辑该问题。