5

I'm working on a Visual Studio extension, when the ToggleEnabled() function is called, it triggers OnToggleEnabled -> OnToggleEnabledHandler.

At private void OnToggleEnabledHandler() these attempts are not working, and the UI is not being updated.

I'm trying to make it call the LayoutChangedHandler function.

How I could "force" Visual Studio to update its UI?

-Walkthrough-

A shortcut send to VS trigger the Execute(AsyncPackage package) function, and it trigger the ToggleEnabled() function:

    public class ImageAdornmentManager : ITagger<ErrorTag>, IDisposable
    {
        private readonly IAdornmentLayer _layer;
        private readonly IWpfTextView _view;
        private readonly VariableExpander _variableExpander;
        private string _contentTypeName;
    
        public static bool Enabled { get; set; }
        public static event Action OnToggleEnabled;
    
        static ImageAdornmentManager()
        {
            Enabled = true;
        }
    
        public static void ToggleEnabled()
        {
            ThreadHelper.ThrowIfNotOnUIThread();
    
            Enabled = !Enabled;
            UIMessage.Show($"Comment: {Enabled}. Scroll editor window to update.");
            OnToggleEnabled?.Invoke();
        }
    
        public ImageAdornmentManager(IWpfTextView view)
        {
            _view = view;
            _layer = view.GetAdornmentLayer("ImageCommentLayer");
            _view.LayoutChanged += LayoutChangedHandler;
            _contentTypeName = view.TextBuffer.ContentType.TypeName;
            _view.TextBuffer.ContentTypeChanged += contentTypeChangedHandler;
    
            OnToggleEnabled += OnToggleEnabledHandler;
        }
            
        private void OnToggleEnabledHandler()
        {
            Console.Write("OnToggleEnabledHandler");
            _view.VisualElement.InvalidateMeasure();
            _view.VisualElement.InvalidateArrange();
            _view.VisualElement.InvalidateVisual();
            _view.LayoutChanged += (sender, e) => { };
        }
    }
Cesar
  • 41
  • 2
  • 5
  • 16
  • When and where are you calling `ToggleEnabled` from? BTW, you really should not be using `static` for events here, especailly as you don't seen to be un-registering event-handlers when no-longer needed - you'll end-up leaking strong-references. – Dai Jul 19 '23 at 17:35
  • @Dai from a shortcut, ill add this info to the quest – Cesar Jul 19 '23 at 17:45

1 Answers1

0

I could be totally missing something here, but re

"How I could "force" Visual Studio to update its UI?"

don't you just need to call update() on the layer? e.g.

    private void OnToggleEnabledHandler()
    {
        ...
        _layer.Update(); // Updates the layout and redraws

    }

See the AdornerLayer.Update Method

re

"I'm trying to make it call the LayoutChangedHandler function."

The ITextView.LayoutChanged Event is raised whenever the view does a layout. I am pretty sure calling update on the layer will cause the view to do this...although I could be wrong.

Fraser
  • 15,275
  • 8
  • 53
  • 104
  • I have add `using System.Windows.Documents;` and when i try `_layer.Update()` i'm getting this error: `'IAdornmentLayer' does not contain a definition for 'Update' and no accessible extension method 'Update' accepting a first argument of type 'IAdornmentLayer' could be found (are you missing a using directive or an assembly reference?)` – Cesar Jul 24 '23 at 14:00