Skip to content

Instantly share code, notes, and snippets.

@FooBartn
Last active November 4, 2022 11:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FooBartn/dd62af73ed5703f121ea5688243009cb to your computer and use it in GitHub Desktop.
Save FooBartn/dd62af73ed5703f121ea5688243009cb to your computer and use it in GitHub Desktop.
# Note: Edited from
# https://foxdeploy.com/resources/ise-snippets/xaml-to-gui/ original XAML loader script
# Change contents of here string to your XAML code from Visual Studio
$RawXAML = @"
Enter XAML contents here
"@
[void][System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework')
[xml]$XAML = $RawXAML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
#Read XAML
$XAMLReader= New-Object System.Xml.XmlNodeReader $XAML
try{
$Form=[Windows.Markup.XamlReader]::Load($XAMLReader)
} catch {
Throw "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
}
#===========================================================================
# Load XAML Objects In PowerShell
#===========================================================================
$XAML.SelectNodes("//*[@Name]") |
ForEach-Object {
Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)
}
Get-Variable WPF* # Will show you the form variables
#===========================================================================
# Events
#===========================================================================
#===========================================================================
# Shows the form
#===========================================================================
$Form.ShowDialog() | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment