Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Convert-XAMLtoWindow
- {
- param
- (
- [Parameter(Mandatory=$true)]
- [string]
- $XAML,
- [string[]]
- $NamedElements,
- [switch]
- $PassThru
- )
- Add-Type -AssemblyName PresentationFramework
- $reader = [System.XML.XMLReader]::Create([System.IO.StringReader]$XAML)
- $result = [System.Windows.Markup.XAMLReader]::Load($reader)
- foreach($Name in $NamedElements)
- {
- $result | Add-Member NoteProperty -Name $Name -Value $result.FindName($Name) -Force
- }
- if ($PassThru)
- {
- $result
- }
- else
- {
- $result.ShowDialog()
- }
- }
- $xaml = @'
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- MinHeight="350"
- Width="525"
- SizeToContent="Height"
- Title="PowerShell WPF Window"
- Topmost="True">
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="30*"/>
- <ColumnDefinition Width="30*"/>
- <ColumnDefinition Width="30*"/>
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height="20*"/>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="20*"/>
- </Grid.RowDefinitions>
- <Button
- Name="SEP"
- Width="80"
- Height="25"
- HorizontalAlignment="Right"
- Margin="0,0,82.667,125"
- VerticalAlignment="Bottom">SEP
- </Button>
- </Grid>
- </Window>
- '@
- $window = Convert-XAMLtoWindow -XAML $xaml -NamedElements 'SEP' -PassThru
- $window.SEP.add_Click(
- {
- [System.Object]$sender = $args[0]
- [System.Windows.RoutedEventArgs]$e = $args[1]
- }
- )
- $window.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement