c# – 突出显示AvalonEdit中所有选定单词的出现次数

前端之家收集整理的这篇文章主要介绍了c# – 突出显示AvalonEdit中所有选定单词的出现次数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在AvalonEdit中突出显示所有选定单词.我创建了一个HihglinghtingRule类的实例:
var rule = new HighlightingRule()
   {
       Regex = regex,//some regex for finding occurences
       Color = new HighlightingColor {Background = new SimpleHighlightingBrush(Colors.Red)}
   };

我该怎么办?
谢谢.

解决方法

要使用HighlightingRule,您必须创建突出显示引擎的另一个实例(HighlightingColorizer等)

编写一个突出显示单词的DocumentColorizingTransformer更容易,更有效:

public class ColorizeAvalonEdit : DocumentColorizingTransformer
{
    protected override void ColorizeLine(DocumentLine line)
    {
        int lineStartOffset = line.Offset;
        string text = CurrentContext.Document.GetText(line);
        int start = 0;
        int index;
        while ((index = text.IndexOf("AvalonEdit",start)) >= 0) {
            base.ChangeLinePart(
                lineStartOffset + index,// startOffset
                lineStartOffset + index + 10,// endOffset
                (VisualLineElement element) => {
                    // This lambda gets called once for every VisualLineElement
                    // between the specified offsets.
                    Typeface tf = element.TextRunProperties.Typeface;
                    // Replace the typeface with a modified version of
                    // the same typeface
                    element.TextRunProperties.SetTypeface(new Typeface(
                        tf.FontFamily,FontStyles.Italic,FontWeights.Bold,tf.Stretch
                    ));
                });
            start = index + 1; // search for next occurrence
        }
    }
}

猜你在找的C#相关文章