我正在尝试使用AvalonEdit创建自定义超链接。我创建了一个发电机(基于样品),其识别语法,我可以设置一个URI:使用AvalonEdit的自定义超链接
public class LinkGenerator : VisualLineElementGenerator
{
readonly static Regex imageRegex = new Regex(@"<mylink>", RegexOptions.IgnoreCase);
public LinkGenerator()
{}
Match FindMatch(int startOffset)
{
// fetch the end offset of the VisualLine being generated
int endOffset = CurrentContext.VisualLine.LastDocumentLine.EndOffset;
TextDocument document = CurrentContext.Document;
string relevantText = document.GetText(startOffset, endOffset - startOffset);
return imageRegex.Match(relevantText);
}
/// Gets the first offset >= startOffset where the generator wants to construct
/// an element.
/// Return -1 to signal no interest.
public override int GetFirstInterestedOffset(int startOffset)
{
Match m = FindMatch(startOffset);
return m.Success ? (startOffset + m.Index) : -1;
}
/// Constructs an element at the specified offset.
/// May return null if no element should be constructed.
public override VisualLineElement ConstructElement(int offset)
{
Match m = FindMatch(offset);
// check whether there's a match exactly at offset
if (m.Success && m.Index == 0)
{
var line = new VisualLineLinkText(CurrentContext.VisualLine, m.Length);
line.NavigateUri = new Uri("http://google.com");
return line;
}
return null;
}
}
但是有两个问题,我似乎无法弄清楚:
什么我传递给VisualLineLinkText构造函数来简化文字说“MyLink”?
我在哪里放一个事件处理程序,它将接收RequestNavigateEventArgs,这样我就可以覆盖点击行为了?
对不起,深入挖掘这个旧的答案,但“跳转到定义”风格导航正是我想要实现的。我只是不明白在使用VisualLineText时发生的“匹配”。我可以以某种方式将它连接到突出显示引擎? – themightylc 2017-10-30 07:38:41
不要以上评论。语法突出显示在CreateInstance Sub中 – themightylc 2017-10-30 07:47:06