Advertisement
Combreal

simpleXAMLWindow.ps1

Mar 20th, 2021
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #create a form (Visual c#/Windows Form/WPF App(.NET Framlework)) in VS Studio Community
  2. #name the needed widgets (just add Name="widgetName" in the widget beacon
  3. #copy and paste the xaml file's path in the script bellow
  4. #finally add the logic before showing the Dialog
  5.  
  6. Add-Type -AssemblyName PresentationFramework
  7. $xamlFile = "C:\Users\¢ombre\source\repos\testWPF01\testWPF01\MainWindow.xaml"
  8. $inputXML = Get-Content $xamlFile -Raw
  9. $inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'
  10. [XML]$XAML = $inputXML
  11. $reader = (New-Object System.Xml.XmlNodeReader $xaml)
  12. try {
  13.     $window = [Windows.Markup.XamlReader]::Load( $reader )
  14. } catch {
  15.     Write-Warning $_.Exception
  16.     throw
  17. }
  18. $xaml.SelectNodes("//*[@Name]") | ForEach-Object {
  19.     try {
  20.         Set-Variable -Name "var_$($_.Name)" -Value $window.FindName($_.Name) -ErrorAction Stop
  21.     } catch {
  22.         throw
  23.     }
  24. }
  25. #Get-Variable var_*
  26.  
  27. $var_btn.Add_Click( {
  28.    $var_txtBox.Text = ""})
  29.  
  30. $Null = $window.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement