我正在视觉工作室进行扩展。但使用IElisionBuffer和IDifferenceBuffer时出错。 以下是我的代码。
public ViewportAdornment1(IWpfTextView view, IProjectionBufferFactoryService projectionBufferFactory, IDifferenceBufferFactoryService differenceBufferFactory)
{
_projectionBufferFactory = projectionBufferFactory;
_differenceBufferFactory = differenceBufferFactory;
_view = view;
NormalizedSnapshotSpanCollection span = new NormalizedSnapshotSpanCollection(_view.TextSnapshot, new Span(5, 10));
_buffer = _projectionBufferFactory.CreateElisionBuffer(null, span, ElisionBufferOptions.FillInMappingMode);
IProjectionSnapshot snapshot = _buffer.ElideSpans(span);
IDifferenceBuffer differenceBuffer = _differenceBufferFactory.CreateDifferenceBuffer(_view.VisualSnapshot.TextBuffer, snapshot.TextBuffer);
differenceBuffer.SnapshotDifferenceChanged += ApplySnapshotDifference;
}
private void ApplySnapshotDifference(object sender, SnapshotDifferenceChangeEventArgs e)
{
ISnapshotDifference snapshotDifference = e.After.DifferenceBuffer.CurrentSnapshotDifference;
var diffs = snapshotDifference.LineDifferences;
ITextEdit edit = _buffer.CreateEdit(EditOptions.DefaultMinimalChange, null, "myedit");
foreach (var diff in diffs)
{
edit.Replace(diff.Left, diff.After.ToString());
}
edit.Apply();
}
下面似乎是内部异常,但我不知道出了什么问题。
IDifferenceBuffer differenceBuffer = _differenceBufferFactory.CreateDifferenceBuffer(_view.VisualSnapshot.TextBuffer, snapshot.TextBuffer);
MSDN备注:“差异是在后台线程中计算的 响应各种变化(文本更改,选项更改等), 虽然围绕差异的所有事件,如 SnapshotDifferenceChanged将在拥有该主机的线程上引发 LeftBuffer和RightBuffer(通常是UI线程)。“
(我无法上传图片)错误弹出窗口说:
未处理的类型异常 mscorlib.dll中发生'System.Reflection.TargetInvocationException'
附加信息:目标是抛出异常 调用
P.S。 我的目标是制作一个链接到某些文本的装饰框,例如错误弹出窗口(但替换文本)。有没有比使用IElisionBuffer更容易实现的方法?