Visual Studio MEF Extension - Force Margin Glyphs

2020-03-15 00:58发布

问题:

The extension I am writing started life from the Walkthrough: Creating a Margin Glyph example provided by Microsoft: https://msdn.microsoft.com/en-us/library/ee361745.aspx

Everything works fine, except I am trying to trigger the redrawing or updating of the margin glyphs whenever the data I use for deciding which line to draw a glyph on changes.

Basically I have a button in a tool window, when the user clicks the button, I want a glyph to appear in the margin. Right now, it does not appear until you scroll away and back, or obviously close and reload the document.

I have researched this, and have seen some code samples that use IViewTaggerProvider instead of ITaggerProvider. In those cases it appeared that a handler for a LayoutChanged event is able to be added to the ITagger derived class which also contains the GetTags method. However I was unable to successfully switch the code from using ITaggerProvider to IViewTaggerProvider, the extension crashed when run. I don't know if this is a change that will lead to the solution, or if it's unnecessary.

How can I force the glyph to get rendered? Can I raise an event of some kind to force the GetTags code to be exercised? How would I raise that event from outside of the class? From the tool window for example?

回答1:

You were on the right path, changing from ITaggerProvider to IViewTaggerProvider allowed me to redraw the glyphs. I used the Implementing a Brace Matching Tagger Provider section in Walkthrough: Displaying Matching Braces example to make these changes.

Using the IViewTaggerProvider, you can then call

TagsChanged?.Invoke(this, new SnapshotSpanEventArgs(
                              new SnapshotSpan(
                                      SourceBuffer.CurrentSnapshot,
                                      0, 
                                      SourceBuffer.CurrentSnapshot.Length)));

in your functions to explicitly call GetTags and go over the spans in the current snapshot.