Я пытаюсь создать пользовательские гиперссылки с помощью AvalonEdit. Я создал генератор (на основе образца), который распознает синтаксис, и я могу установить Uri:
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, чтобы я мог переопределить поведение щелчка?