Powershell 使用Xaml中的变量进行计算
在学习PowerShell时,我需要使用Xaml来显示我的一个小项目的结果。有人建议我在最后使用xaml作为总结结果,我正在努力在xaml中进行计算。在经历了很多之后,我能够运行下面所示的代码,这到目前为止还不算太糟糕Powershell 使用Xaml中的变量进行计算,powershell,xaml,variables,calculation,Powershell,Xaml,Variables,Calculation,在学习PowerShell时,我需要使用Xaml来显示我的一个小项目的结果。有人建议我在最后使用xaml作为总结结果,我正在努力在xaml中进行计算。在经历了很多之后,我能够运行下面所示的代码,这到目前为止还不算太糟糕 Clear-Host #Load Assembly and Library Add-Type -AssemblyName PresentationFramework [xml]$Form = @" <Window xmlns="http://schemas.
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()
清除主机
#加载程序集和库
添加类型-AssemblyName PresentationFramework
[xml]$Form=@”
Te
按钮5
按钮6
按钮7
按钮8
按钮9
"@
#创建表单
$XMLReader=(新对象System.Xml.XmlNodeReader$Form)
$XMLForm=[Windows.Markup.XamlReader]::加载($XMLReader)
#显示XMLform
$XMLForm.ShowDialog()
我现在面临的问题是我正在运行的循环,在将代码添加到xaml代码之前,我正在准备代码。
所以我试着做以下的测试:
<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" />
目标是计算“$Script:Counter+1”,因为$Script:Counter有一个数字,而它是由一加起来的。
我该如何以这样的方式编写代码,例如“$Script:Counter+1”将变为2,并且不会出现错误消息
我尝试了“variable”、“variable”、“{variable}”和其他尝试,但失败了
我做错了什么?清除主机
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()
#加载程序集和库
添加类型-AssemblyName PresentationFramework
对于($i=0;$i-lt 5;$i++){
$Looped+='
“-f$i
}
[xml]$Form=@”
$Looped
Te
按钮5
按钮6
按钮7
按钮8
按钮9
"@
#创建表单
$XMLReader=(新对象System.Xml.XmlNodeReader$Form)
$XMLForm=[Windows.Markup.XamlReader]::加载($XMLReader)
#显示XMLform
$XMLForm.ShowDialog()
使用For循环生成“Grid.Column”字符串
然后将其插入到Xaml文本中您需要使用子表达式操作符
$()
。将$Script:Counter+1
替换为$($Script:Counter+1)
。原因是在非文字字符串中,变量扩展将在变量名的第一个非法字符处停止。在本例中,这就是:
,它是范围修饰符脚本的一部分:
。由于here字符串@”“@
创建了此条件,因此可以按如下方式更新代码部分:
$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
"@
$Form=[xml]@”
#其他代码
#其他代码
"@
您需要使用子表达式运算符$()
。将$Script:Counter+1
替换为$($Script:Counter+1)
。原因是在非文字字符串中,变量扩展将在变量名的第一个非法字符处停止。在这种情况下,这就是:
。嘿,这就是解决方案,正是我所需要的,thanx非常感谢您的帮助,非常感谢您确实需要$(…)
,但不是因为:
,而是因为要插入的是一个表达式,而不仅仅是一个变量引用<代码>$script:foo='bar';例如,“value:$script:foo”可以很好地工作——请参阅Thanx,现在正在进行这方面的工作