bsod_
bsod_

Reputation: 941

XAML WPF GUI in Powershell

I am currently building a powershell program that pulls OS information into a GUI. I am currently having an issue with what i think is invalid xaml code -

[void] [System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="OS Details" Height="384.399" Width="525">
<Grid Margin="0,0,0,-12">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="281*"/>
        <ColumnDefinition Width="236*"/>
    </Grid.ColumnDefinitions>
    <TextBox Name="textBox" Height="37" Margin="0,1,0,0" TextWrapping="Wrap"    Text=" &#x9;               Operating System Details" VerticalAlignment="Top" FontSize="18" Background="#FF9BABF1" HorizontalAlignment="Left" Width="517" Grid.ColumnSpan="2"/>
    <Label Name="HostName" Content="HostName" HorizontalAlignment="Left" Height="35" Margin="0,43,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="OSName" Content="OS Name" HorizontalAlignment="Left" Height="35" Margin="0,83,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="AvailableMemory" Content="Available Memory" HorizontalAlignment="Left" Height="35" Margin="0,123,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="OSArchitecture" Content="OS Architecture" HorizontalAlignment="Left" Height="35" Margin="0,163,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="WindowsDirectory" Content="Windows Directory" HorizontalAlignment="Left" Height="35" Margin="0,203,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="WindowsVersion" Content="Windows Version" HorizontalAlignment="Left" Height="33" Margin="0,243,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <Label Name="SystemDrive" Content="System Drive" HorizontalAlignment="Left" Height="32" Margin="0,281,0,0" VerticalAlignment="Top" Width="246" Background="#FF9BABF1"/>
    <TextBox Name="txtHostname" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,43,0,0" TextWrapping="Wrap" Text="&#xD;&#xA;" VerticalAlignment="Top" Width="254" TextChanged="textBox1_TextChanged"/>
    <TextBox Name="txtOSName" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,83,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <TextBox Name="txtAvailMem" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,123,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <TextBox Name="txtOSArch" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,163,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <TextBox Name="txtWinDir" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,203,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <TextBox Name="txtWinVer" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,241,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <TextBox Name="txtSysDrv" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="32" Margin="253,281,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="254"/>
    <Button Name="button" Grid.ColumnSpan="2" Content="Exit" HorizontalAlignment="Left" Height="27" Margin="10,318,0,0" VerticalAlignment="Top" Width="490"/>

</Grid>
</Window>

'@

#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible     causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}

When i run this i get the "catch" statement (code was written by another user on another helpful site - https://blogs.technet.microsoft.com/platformspfe/2014/01/20/integrating-xaml-into-powershell/#comment-3105)

Any ideas on where i am going wrong?

Thanks in advance!

Upvotes: 0

Views: 1022

Answers (3)

Jim Moyle
Jim Moyle

Reputation: 665

As Asnivor states, you can't use an event in your xaml like that.

One option would be to take out the TextChanged="textBox1_TextChanged" from the xaml and add the event to your code behind.

$txtHostname.add_textchanged({

    Your code here

})

Upvotes: 0

Mike Garuccio
Mike Garuccio

Reputation: 2718

TextChanged="textBox1_TextChanged" is the problem, there is no textBox1 so that event is not able to generate. You should be able to just safely remove it though, once you load the control into your script you'll be able to define a $txtHostName.Add_TextChanged in your code.

Upvotes: 0

Asnivor
Asnivor

Reputation: 266

When I try to use this xaml code in an actual WPF project I get the following error on build:

Error 'Window' root element requires a x:Class attribute to support event handlers in the XAML file. Either remove the event handler for the TextChanged event, or add a x:Class attribute to the root element. Line 20 Position 190

This is the problem line:

<TextBox Name="txtHostname" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="35" Margin="253,43,0,0" TextWrapping="Wrap" Text="&#xD;&#xA;" VerticalAlignment="Top" Width="254" TextChanged="textBox1_TextChanged"/>

If you dont need that TextChanged event to be there try deleting it and see if that fixes the issue.

Upvotes: 1

Related Questions