Advertisement
bbalz

Untitled

May 27th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1.  
  2.  
  3.  
  4. function Convert-XAMLtoWindow
  5. {
  6. param
  7. (
  8. [Parameter(Mandatory=$true)]
  9. [string]
  10. $XAML,
  11.  
  12. [string[]]
  13. $NamedElements,
  14.  
  15. [switch]
  16. $PassThru
  17. )
  18.  
  19. Add-Type -AssemblyName PresentationFramework
  20.  
  21. $reader = [System.XML.XMLReader]::Create([System.IO.StringReader]$XAML)
  22. $result = [System.Windows.Markup.XAMLReader]::Load($reader)
  23. foreach($Name in $NamedElements)
  24. {
  25. $result | Add-Member NoteProperty -Name $Name -Value $result.FindName($Name) -Force
  26. }
  27.  
  28. if ($PassThru)
  29. {
  30. $result
  31. }
  32. else
  33. {
  34. $result.ShowDialog()
  35. }
  36. }
  37.  
  38. $xaml = @'
  39. <Window
  40. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  41. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  42. MinHeight="350"
  43. Width="525"
  44. SizeToContent="Height"
  45. Title="PowerShell WPF Window"
  46. Topmost="True">
  47. <Grid>
  48. <Grid.ColumnDefinitions>
  49. <ColumnDefinition Width="30*"/>
  50. <ColumnDefinition Width="30*"/>
  51. <ColumnDefinition Width="30*"/>
  52. </Grid.ColumnDefinitions>
  53. <Grid.RowDefinitions>
  54. <RowDefinition Height="20*"/>
  55. <RowDefinition Height="Auto"/>
  56. <RowDefinition Height="20*"/>
  57. </Grid.RowDefinitions>
  58. <Button
  59. Name="SEP"
  60. Width="80"
  61. Height="25"
  62. HorizontalAlignment="Right"
  63. Margin="0,0,82.667,125"
  64. VerticalAlignment="Bottom">SEP
  65. </Button>
  66. </Grid>
  67. </Window>
  68. '@
  69.  
  70. $window = Convert-XAMLtoWindow -XAML $xaml -NamedElements 'SEP' -PassThru
  71.  
  72.  
  73. $window.SEP.add_Click(
  74. {
  75. [System.Object]$sender = $args[0]
  76. [System.Windows.RoutedEventArgs]$e = $args[1]
  77.  
  78. }
  79. )
  80.  
  81.  
  82. $window.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement