Learning PowerShell, I need to use Xaml to show results for a small project of mine. I have been suggested to use xaml for a summary result at the end and am struggling with a calculation within xaml. After a lot of experiencing, I was able to get the code run as seen below, which is so far not too bad.
Clear-Host
#Load Assembly and Library
Add-Type -AssemblyName PresentationFramework
[xml]$Form = @"
<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"
Title="MainWindow" Height="1000" Width="1000">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel VerticalAlignment="Top">
<TextBlock FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="28" Foreground="Blue" HorizontalAlignment="Center" Text="Vocabulary Test Results" /> <Separator />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<TextBlock FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center" Text="Question Nr:" />
<TextBlock Grid.Column="1" FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center" Text="Asked Question" />
<TextBlock Grid.Column="2" FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center" Text="Expected Answer" />
<TextBlock Grid.Column="3" FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center" Text="Your Answer" />
<TextBlock Grid.Column="4" FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center" Text="Result" />
<TextBlock Grid.Column="0" Grid.Row="1" FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" HorizontalAlignment="Center">
Te<Run Foreground="Red" Text="st"></Run>
</TextBlock>
<Button Grid.Column="1" Grid.Row="1">Button 5</Button>
<Button Grid.Column="2" Grid.Row="1">Button 6</Button>
<Button Grid.Row="2">Button 7</Button>
<Button Grid.Column="1" Grid.Row="2">Button 8</Button>
<Button Grid.Column="2" Grid.Row="2">Button 9</Button>
</Grid>
</StackPanel>
</ScrollViewer>
</Window>
"@
#Create a form
$XMLReader = (New-Object System.Xml.XmlNodeReader $Form)
$XMLForm = [Windows.Markup.XamlReader]::Load($XMLReader)
#Show XMLform
$XMLForm.ShowDialog()
The issue I am facing now is with a loop I am running, where I am preparing the code before adding it into the xaml code. So what I try to do is the following for a test:
<TextBlock Grid.Column="$Script:Counter+1" Grid.Row="$Script:Counter+1" FontWeight='Bold' Margin='10' FontFamily='Arial' FontSize='18' Foreground='Black' Background='#65D260' HorizontalAlignment="Center" Text="$Script:Counter+1" />
Target is, to have "$Script:Counter+1" 'calculated' because $Script:Counter has a number, while it is added by one. How would I have to write the code on such way, that for example "$Script:Counter+1" will become 2 and I do not end up with an error message?
I tried with ""variable"", "(variable)", "{variable}" and other attempts but failed.
What am I doing wrong?