文本块中的 Xaml 变量



学习PowerShell,有人建议我解决Xaml的问题。完全不了解 Xaml,我能够按照我期望的方式为我的私人项目构建一个窗口。

使用Xaml和下面的代码,我能够生成一个窗口并在其中放置一个表。这个想法是稍后,循环将构建表,到目前为止它也在运行。

Clear-Host
$Test = "Vocabulary Test Results"
$AnotherTest = '<TextBlock FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="28" Foreground="Blue" HorizontalAlignment="Center" Text="$Test" />  <Separator />'
#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">  
$AnotherTest
<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*" />
<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" />

<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>

</Grid>
</StackPanel>  
</ScrollViewer>  
</Window> 
"@
#Create a form
$XMLReader = (New-Object System.Xml.XmlNodeReader $Form)
$XMLForm = [Windows.Markup.XamlReader]::Load($XMLReader)
#Show XMLform
[void]$XMLForm.ShowDialog()

我现在正在解决的问题是包含此代码的行:

$AnotherTest = '<TextBlock FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="28" Foreground="Blue" HorizontalAlignment="Center" Text="$Test" />  <Separator />'

我试图在"文本"后面添加一个变量,但尝试了各种事情,我无法以文本形式接收"词汇测试结果"。要么我有错误消息,要么最终得到一个空白字段,要么看到变量代码而不是预期结果。

我尝试了几个想法,例如

Text="$($Test")
Text=$Test
Text=""$Test""
Text={$Test}

等等。这些都没有返回预期的结果。 试图在网上找到一些解决方案,我确实看到了一些绑定的例子,但必须承认,我并不完全理解它是如何工作的。

有没有一种简单的方法可以解决这个问题,或者没有办法绕过绑定,如果是,我将如何做到这一点?

感谢您的任何帮助和建议

"..."引用
  • (双引号(是变量引用所必需的(例如$text(或子表达式(例如,$($text + '!')(嵌入在这样的字符串中以扩展(内插(,即用它们的值替换。

    • 相比之下,'...'(单引号(字符串的内容是逐字解释的 - 不会发生扩展。
  • 如果需要在"..."字符串中嵌入文字"字符,则需要对它们(每个(进行转义,这可以通过以下两种方式之一完成:

    • `",这是可取的,因为`通常充当PowerShell的转义字符;例如:
      • "Nat `"King`" Cole"
    • 或者,仅在"..."内部,您可以使用"".
      • "Nat ""King"" Cole"
  • 如果您使用可扩展的here-string(@"<newline>...<newline>"@(,则可以避免这种转义的需要,因为您已经用于分配给$Form变量;请注意,结束分隔符"@必须在其自己的行上并且位于该行的最开头才能被识别。

请参阅about_Quoting_Rules。

应用于你的方案:

  • 作为可扩展字符串,带有转义:
$AnotherTest = "<TextBlock FontWeight=`"Bold`" ... Text=`"$Test`" />  <Separator />"
  • 作为可扩展的here 字符串,无需转义:
$AnotherTest = @"
<TextBlock FontWeight="Bold" Margin="10" FontFamily="Arial" FontSize="28" Foreground="Blue" HorizontalAlignment="Center" Text="$Test" />  <Separator />
"@

最新更新