Forked from QuietusPlus/PowerShell-XAML-Template.ps1
Last active
September 23, 2022 15:53
-
-
Save figueroadavid/1ab5d677896d69be9f1f21341e9a6f14 to your computer and use it in GitHub Desktop.
Template: Use PowerShell to launch a .xaml file (MainWindow.xaml) designed within Visual Studio. It automatically removes attributes which are otherwise incompatible, so design in Visual Studio and launch your GUI without any additional steps. The template supports "Windows Presentation Foundation" and "Windows Forms" by default, add additional …
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
| [cmdletbinding()] | |
| param( | |
| [parameter(Mandatory, ValueFromPipelineByPropertyName)] | |
| [string]$FilePath | |
| ) | |
| # .NET Framework classes | |
| Add-Type -AssemblyName PresentationFramework | |
| Add-Type -AssemblyName System.Windows.Forms | |
| # XAML | |
| [xml]$XAML = [xml]::New() | |
| $XAML.Load($FilePath) | |
| $XAML.Window.RemoveAttribute('x:Class') | |
| $XAML.Window.RemoveAttribute('mc:Ignorable') | |
| $XAMLReader = New-Object System.Xml.XmlNodeReader $XAML | |
| $MainWindow = [Windows.Markup.XamlReader]::Load($XAMLReader) | |
| # UI Elements | |
| $XAML.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $MainWindow.FindName($_.Name)} | |
| <# | |
| Functions | |
| #> | |
| <# | |
| Initialisation | |
| #> | |
| # Show MainWindow | |
| $null = $MainWindow.ShowDialog() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment