How to force Visual Studio to update the UI?

326 views Asked by At

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) => { };
        }
    }
1

There are 1 answers

1
Fraser On

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.