回答并记录我的曲折经历。
扩展 IIntellisenseControllerProvider:
[Export(typeof(IIntellisenseControllerProvider))]
[ContentType("Disassembly")]
[TextViewRole(PredefinedTextViewRoles.Debuggable)]
internal sealed class MyInfoControllerProvider : IIntellisenseControllerProvider
{
[Import]
private IQuickInfoBroker _quickInfoBroker = null;
[Import]
private IToolTipProviderFactory _toolTipProviderFactory = null;
public IIntellisenseController TryCreateIntellisenseController(ITextView textView, IList<ITextBuffer> subjectBuffers)
{
var provider = this._toolTipProviderFactory.GetToolTipProvider(textView);
return new MyInfoController(textView, subjectBuffers, this._quickInfoBroker, provider);
}
}
MyInfoController 的构造函数如下所示:
internal MyInfoController(
ITextView textView,
IList<ITextBuffer> subjectBuffers,
IQuickInfoBroker quickInfoBroker,
IToolTipProvider toolTipProvider)
{
this._textView = textView;
this._subjectBuffers = subjectBuffers;
this._quickInfoBroker = quickInfoBroker;
this._toolTipProvider = toolTipProvider;
this._textView.MouseHover += this.OnTextViewMouseHover;
}
OnTextViewMouseHover 处理弹出窗口的创建:
private void OnTextViewMouseHover(object sender, MouseHoverEventArgs e)
{
SnapshotPoint? point = GetMousePosition(new SnapshotPoint(this._textView.TextSnapshot, e.Position));
if (point.HasValue)
{
string contentType = this._textView.TextBuffer.ContentType.DisplayName;
if (contentType.Equals("Disassembly"))
{
this.ToolTipLegacy(point.Value);
}
else // handle Tooltip the regular way
{
if (!this._quickInfoBroker.IsQuickInfoActive(this._textView))
{
ITrackingPoint triggerPoint = point.Value.Snapshot.CreateTrackingPoint(point.Value.Position, PointTrackingMode.Positive);
this._session = this._quickInfoBroker.CreateQuickInfoSession(this._textView, triggerPoint, false);
this._session.Start();
}
}
}
}
在 ToolTipLegacy 中处理工具提示的创建:
private void ToolTipLegacy(SnapshotPoint triggerPoint)
{
// retrieve what is under the triggerPoint, and make a trackingSpan.
var textBlock = new TextBlock() { Text = "Bla" };
textBlock.Background = TODO; //do not forget to set a background
this._toolTipProvider.ShowToolTip(trackingSpan, textBlock, PopupStyles.DismissOnMouseLeaveTextOrContent);
}