gpt4 book ai didi

c# - TextHighlighter RichTextBlock UWP

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:00 24 4
gpt4 key购买 nike

我想用 TextHighlighter 对象突出显示 richtextblock 的一些文本。我创建了一个 TextRange,将其添加到列表中,然后创建了一个新的 TextHighlighter 实例并设置了背景颜色。但是现在我不能使用 TextHighlighter 来突出显示文本。我应该如何进行?

xaml:

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="300">
<RichTextBlock x:Name="RichFullText" Margin="0,0,0,10">
<Paragraph x:Name="Testo">
<Run Foreground="Blue" FontSize="24" FontStyle="Italic">
This is a
</Run>
<Run Foreground="Teal" FontFamily="Segoe UI Light" FontSize="18" >
example text
</Run>
<Run Foreground="Black" FontFamily="Arial" FontSize="14" FontWeight="Bold">
format
</Run>
</Paragraph>
</RichTextBlock>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
<TextBox x:Name="txbToFind" Height="32" VerticalAlignment="Bottom" Width="200" HorizontalAlignment="Left"/>
<Button x:Name="btnToFind" Content="Find" Click="btnToFind_Click" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</StackPanel>
</Grid>

xaml.cs:

public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}

private void btnToFind_Click(object sender, RoutedEventArgs e)
{
TextRange textRange = new TextRange() { StartIndex = 3, Length = 5 };
List<TextRange> rangelist = new List<TextRange>();
rangelist.Add(textRange);
TextHighlighter evidenziatore = new TextHighlighter() { Background = new SolidColorBrush(Colors.Yellow) };
//RichFullText... There I would apply highlight to RichTextBlock
}
}

如何使用 TextHighlighter 突出显示?

最佳答案

您需要将 TextHighlighter 添加到 RichTextBlockTextHighlighters 集合中:

TextRange textRange = new TextRange() { StartIndex = 3, Length = 10 };            
TextHighlighter highlighter = new TextHighlighter()
{
Background = new SolidColorBrush(Colors.Yellow),
Ranges = { textRange }
};
//add the highlighter
RichBlock.TextHighlighters.Add(highlighter);

另请注意,我已将 textRange 添加到您的 TextHighlighter 实例中的 Ranges 集合中。

关于c# - TextHighlighter RichTextBlock UWP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48477203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com