Read XAML with power shell

powershell xaml

502 观看


131 作者的声誉

I have an XAML file below which is created by Visual Studio, I'm unable to read the nodes using powershell using name attribute.

I have tried below command to read from $XAML

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

$xaml.SelectNodes("//*[@Name]") | ForEach-Object {Set-Variable -Name ($.Name) -Value $Form.FindName($_.Name)}

Above mentioned commands were working perfectly with another XAML file, now I don't get any values from the below pasted XAML, difference with this XAML I could see is the addition of grid. however I need to use below XAML. any help is appreciated.

my XAML file available Here

作者: Pr Mod 的来源 发布者: 2017 年 12 月 27 日

回应 (2)


51 作者的声誉


Well, seems like some namespace stuff, can you just use this:

$XAML.GetElementsByTagName("*")|? { $Form.FindName($_.Name) }|% { Set-Variable -Name $_.Name -Value $_ }
作者: smilinger 发布者: 27.12.2017 08:15


131 作者的声誉

Here I am pasting complete working code so that it help some one else with Power shell and WPF

Add-Type -AssemblyName presentationframework,presentationcore

parse your Visual Studio Created XAML here below $Inputxaml = @' Replace your XAML file '@

Run Below clean up step as few of the strings created by VS are not accepted by powershell $CleanUp = $Inputxaml -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace 'x:Class=".*?"','' -replace 'd:DesignHeight="\d*?"','' -replace 'd:DesignWidth="\d*?"','' [xml]$xaml = $CleanUp

$wpf =@{} $reader=(New-Object System.Xml.XmlNodeReader $xaml) $Form=[Windows.Markup.XamlReader]::Load( $reader )

Run through XAML nodes and create name Nodes $namenodes = $XAML.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") $namenodes | ForEach-Object{ $wpf.add($_.Name,$Form.FindName($_.Name))}

Create Event functions $wpf.buttonnew.add_Click({ })

Show GUI $Form.ShowDialog() | out-null

作者: Pr Mod 发布者: 11.01.2018 06:12