Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
April 11, 2024

Defining WPF with XAML

During our exploration of creating a Windows Presentation Foundation (WPF) PowerShell script, I've been taking a programmatic approach. That is, I've been creating instances of the relevant .NET classes in a PowerShell script, updating properties, defining events, and launching the script. I believe this is a good way to teach you how the works. Now that you understand the mechanics, we can move on and separate the form description with the code necessary to run the form. This is the WPF model. ## XAML The traditional approach is to define the visual elements of the form in a special XML format called `XAML`. You can pronounce it *zamel*. For our purposes, the `XAML` definition can be loaded from a file or embedded in the script file. The idea is that you can modify the layout or the code separately. Here's a simple `XAML` file.





        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        WindowStartupLocation="CenterScreen"

        Title="XAML WPF Demo" Height="350" Width="525">

    

         x:Name="label"

            Content="Computername"

            HorizontalAlignment="Left"

            Margin="35,25,0,0"

            VerticalAlignment="Top"/>

         x:Name="ResultsTextBlock"

            HorizontalAlignment="Left"

            Height="191"

            Margin="35,85,0,0"

            TextWrapping="Wrap"

            Text=""

            VerticalAlignment="Top"

            Width="451"

            FontFamily="Consolas"

            />

         x:Name="GoButton"

            Content="Run"

            HorizontalAlignment="Left"

            Margin="323,30,0,0"

            VerticalAlignment="Top"

            Width="75"/>

         x:Name="QuitButton"

            Content="Quit"

            HorizontalAlignment="Left"

            Margin="411,30,0,0"

            VerticalAlignment="Top"

            Width="75"/>

         x:Name="ComputerTextBox"

            HorizontalAlignment="Left"

            Height="21"

            Margin="156,29,0,0"

            TextWrapping="Wrap"

            Text="Enter a computername"

            VerticalAlignment="Top"

            Width="150" />

    


Now that you've seen how we create a WPF form in code, you can probably make sense of the XML file. The first step is to get this definition into our PowerShell script.
Get a premium subscription for full article and archive access

During our exploration of creating a Windows Presentation Foundation (WPF) PowerShell script, I've been taking a programmatic approach. That is, I've been creating instances of the relevant .NET classes in a PowerShell script, updating properties, defining events, and launching the script. I believe this is a good way to teach you how the works. Now that you understand the mechanics, we can move on and separate the form description with the code necessary to run the form. This is the WPF model.

XAML

The traditional approach is to define the visual elements of the form in a special XML format called XAML. You can pronounce it zamel. For our purposes, the XAML definition can be loaded from a file or embedded in the script file. The idea is that you can modify the layout or the code separately.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.