是的,你没有看错,在那里没有提到TextBlock
。
问题是,在WPF中,TextBlock
只是一个更长的Label
,你可以在这里阅读到:
一般认为
Label
适用于短的单行文本(但可能包括图像),而TextBlock
非常适合多行字符串,但只能包含文本(字符串)。
所以你可以像使用Label
一样获取它:
Application application = Application.Launch(applicationPath);
Window window = application.GetWindows().First();
var MyTextBlock = window.Get<Label>("MyTextBlock");
这是我用来测试的XAML标记:
<Window x:Class="WpfApp1.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="103.966" Width="191.724">
<Grid>
<TextBlock x:Name="MyTextBlock" Text="Hello!" Background="CornflowerBlue" Foreground="White" VerticalAlignment="Top"/>
</Grid>
</Window>