Skip to content

Instantly share code, notes, and snippets.

@figueroadavid
Created June 13, 2023 21:22
Show Gist options
  • Select an option

  • Save figueroadavid/5047e1fc2cf46daf680574edff0b3ef3 to your computer and use it in GitHub Desktop.

Select an option

Save figueroadavid/5047e1fc2cf46daf680574edff0b3ef3 to your computer and use it in GitHub Desktop.
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