Created
June 13, 2023 21:22
-
-
Save figueroadavid/5047e1fc2cf46daf680574edff0b3ef3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Show-Xaml { | |
| [cmdletbinding()] | |
| param( | |
| [parameter(Mandatory = $true, ValueFromPipeLineByPropertyName = $true, HelpMessage = 'The path to the XAML file to run')] | |
| [ValidateScript({Test-Path -path $_})] | |
| [string]$FilePath | |
| ) | |
| Add-Type -AssemblyName PresentationFramework -ErrorAction SilentlyContinue | |
| Add-Type -AssemblyName PresentationCore -ErrorAction SilentlyContinue | |
| Add-Type -AssemblyName WindowsBase -ErrorAction SilentlyContinue | |
| $FilePath = Resolve-Path -Path $FilePath | |
| $xaml = New-Object -TypeName XML | |
| $xaml.Load($FilePath) | |
| $xaml.Window.RemoveAttribute('x:Class') | |
| $xaml.Window.RemoveAttribute('mc:Ignorable') | |
| $reader = [System.Xml.XmlNodeReader]::new($xaml) | |
| $Window = [Windows.Markup.XAMLReader]::Load($reader) | |
| $nodes = $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | |
| foreach ($node in $nodes) | |
| { | |
| if (Test-Path Variable:\$($node.name)) | |
| { | |
| Write-Verbose -Message "Variable $($node.name) exists" | |
| } | |
| else | |
| { | |
| New-Variable -Name $node.Name -Value ($Window.FindName($node.name)) | |
| } | |
| } | |
| $null = $Window.ShowDialog() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment