我正在嘗試使用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