Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save figueroadavid/1ab5d677896d69be9f1f21341e9a6f14 to your computer and use it in GitHub Desktop.

Select an option

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 …
[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