Show-Control.ps1 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Displays one or more controls. Controls are piped to Show-Control
  2. function Show-Control {
  3. cmdlet `
  4. -DefaultParameterSet VisualElement
  5. param(
  6. [Parameter(Mandatory=$true,
  7. ParameterSetName="VisualElement",
  8. ValueFromPipeline=$true,
  9. ValueFromPipelineByPropertyName=$true)]
  10. [Windows.Media.Visual]
  11. $control,
  12. [Parameter(Mandatory=$true,
  13. ParameterSetName="Xaml",
  14. ValueFromPipeline=$true,
  15. ValueFromPipelineByPropertyName=$true)]
  16. [string]
  17. $xaml,
  18. [Parameter(ValueFromPipelineByPropertyName=$true,Position=0)]
  19. [Hashtable]
  20. $event,
  21. [Hashtable]
  22. $windowProperties,
  23. # If this switch is set, outputs a psuedoobject of the control and events
  24. [switch]
  25. $outputControl
  26. )
  27. Begin
  28. {
  29. $window = New-Object Windows.Window
  30. $window.SizeToContent = "WidthAndHeight"
  31. if ($windowProperties) {
  32. foreach ($kv in $windowProperties.GetEnumerator()) {
  33. $window."$($kv.Key)" = $kv.Value
  34. }
  35. }
  36. $visibleElements = @()
  37. $windowEvents = @()
  38. }
  39. Process
  40. {
  41. Write-Verbose "Xaml: $xaml"
  42. Write-Verbose "Events: $($event | Out-String)"
  43. switch ($psCmdlet.ParameterSetName)
  44. {
  45. "Xaml" {
  46. $f = [System.xml.xmlreader]::Create([System.IO.StringReader] $xaml)
  47. $visibleElements+=([system.windows.markup.xamlreader]::load($f))
  48. }
  49. "VisualElement" {
  50. $visibleElements+=$control
  51. }
  52. }
  53. if ($event) {
  54. $element = $visibleElements[-1]
  55. foreach ($evt in $event.GetEnumerator()) {
  56. # If the event name is like *.*, it is an event on a named target, otherwise, it's on any of the events on the top level object
  57. if ($evt.Key.Contains(".")) {
  58. $targetName = $evt.Key.Split(".")[1].Trim()
  59. if ($evt.Key -like "Window.*") {
  60. $target = $window
  61. } else {
  62. $target = ($visibleElements[-1]).FindName(($evt.Key.Split(".")[0]))
  63. }
  64. } else {
  65. $target = $visibleElements[-1]
  66. $targetName = $evt.Key
  67. }
  68. $target | Get-Member -type Event |
  69. ? { $_.Name -eq $targetName } |
  70. % {
  71. $eventMethod = $target."add_$targetName"
  72. $eventMethod.Invoke($evt.Value)
  73. }
  74. }
  75. }
  76. }
  77. End
  78. {
  79. if ($visibleElements.Count -gt 1) {
  80. $wrapPanel = New-Object Windows.Controls.WrapPanel
  81. $visibleElements | % { $null = $wrapPanel.Children.Add($_) }
  82. $window.Content = $wrapPanel
  83. } else {
  84. if ($visibleElements) {
  85. $window.Content = $visibleElements[0]
  86. }
  87. }
  88. if ($outputControl) {
  89. $true | Select-Object @{Name='Control';Expression={$visibleElements}},
  90. @{Name='Event';Expression={$event}}
  91. } else {
  92. $null = $window.ShowDialog()
  93. $window.Tag
  94. }
  95. }
  96. }