0

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?

Mike
  • 134
  • 4
  • 12
  • 1
    You need to use the sub-expression operator `$()`. Replace `$Script:Counter+1` with `$($Script:Counter+1)`. The reason is because in a non-literal string, variable expansion will stop at the first illegal character for a variable name. In this case, that is the `:`. – AdminOfThings Sep 24 '19 at 17:35
  • Hey AdminOfThings, that is the solution, exactly what I needed, thanx a lot for the help, much appreciated – Mike Sep 24 '19 at 17:48

2 Answers2

0
Clear-Host
#Load Assembly and Library
Add-Type -AssemblyName PresentationFramework

for ($i = 0 ; $i -lt 5 ; $i++){
    $Looped += '    <TextBlock Grid.Column="{0}" Grid.Row="{0}" FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="18" Foreground="Black" Background="#65D260" HorizontalAlignment="Center" Text="{0}" /> 
' -f $i
}

[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" />
$Looped
    <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()

Use a For loop to generate a string of "Grid.Column" Then insert it into your Xaml text

BiaoGuo
  • 39
  • 3
0

You need to use the sub-expression operator $(). Replace $Script:Counter+1 with $($Script:Counter+1). The reason is because in a non-literal string, variable expansion will stop at the first illegal character for a variable name. In this case, that is the :, which is part of your scope modifier script:. Since the here-string @""@ creates this condition, you can update your section of code like the following:

$Form = [xml]@"
# Other Code
    <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)" />
# Other Code
"@
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
  • Indeed you need `$(...)`, but not because of the `:`, but because what is to be interpolated is an _expression_, not a _mere variable reference_; `$script:foo='bar'; "value: $script:foo"` works just fine, for instance - see https://stackoverflow.com/a/40445998/45375 – mklement0 Sep 25 '19 at 02:21