c# – Visual Studio custom extension does not execute command


I am currently trying on my first custom Visual Studio 2022 Extension. In detail I want to generate a canvas which draws points based on the double values. All this happens in theory when I set in another sourcecode a breakpoint and look at an IEnumerable and check the view with the magnifying glass

I went through the tutorial I found from Microsoft and got everything setup and working to the point where a message popped up aka “Hello World” now I implemented my logic but I can’t get it to work and I don’t know why.
The Button to call it is still here and I get no Error Message

here is my Command.cs

using Microsoft;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Commands;
using Microsoft.VisualStudio.Extensibility.Shell;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Media;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace MTEnumVisualizer
{
    /// <summary>
    /// Command1 handler.
    /// </summary>
    [VisualStudioContribution]
    internal class Command1 : Command
    {
        private readonly TraceSource logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="Command1"/> class.
        /// </summary>
        /// <param name="traceSource">Trace source instance to utilize.</param>
        public Command1(TraceSource traceSource)
        {
            
            this.logger = Requires.NotNull(traceSource, nameof(traceSource));
        }

        /// <inheritdoc />
        public override CommandConfiguration CommandConfiguration => new("%MTEnumVisualizer.Command1.DisplayName%")
        {
            Icon = new(ImageMoniker.KnownValues.Extension, IconSettings.IconAndText),
            Placements = [CommandPlacement.KnownPlacements.ExtensionsMenu],
        };

        /// <inheritdoc />
        public override Task InitializeAsync(CancellationToken cancellationToken)
        {
            this.logger.TraceInformation("Command1 initialized.");
            return base.InitializeAsync(cancellationToken);
        }

        /// <inheritdoc />
        public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
        {
            this.logger.TraceInformation("ExecuteCommandAsync started.");

            Requires.NotNull(context, nameof(context));

            // Retrieve debugger data
            var dataPoints = await GetDebuggerDataPointsAsync(context, cancellationToken);
            if (dataPoints is null || !dataPoints.Any())
            {
                this.logger.TraceInformation("No data points were retrieved from the debugger.");
                return;
            }

            // Determine the highest value and calculate canvas height
            double maxValue = dataPoints.Max();
            double canvasHeight = maxValue * 1.1;

            // Create window
            var window = new Window
            {
                Title = "Data Points Visualization",
                Width = 800,
                Height = 600
            };

            // Create canvas
            var canvas = new Canvas
            {
                Width = 800,
                Height = canvasHeight
            };

            // Draw points on the canvas
            foreach (var point in dataPoints)
            {
                var ellipse = new Ellipse
                {
                    Width = 5,
                    Height = 5,
                    Fill = Brushes.Black
                };

                Canvas.SetLeft(ellipse, point * 100); // Example X-coordinate
                Canvas.SetTop(ellipse, canvasHeight - (point * 100)); // Example Y-coordinate

                canvas.Children.Add(ellipse);
            }

            // Add canvas to window
            window.Content = canvas;

            // Show message
            MessageBox.Show("The window will now be displayed.");

            // Display window
            window.ShowDialog();
        }

        private async Task<IEnumerable<double>> GetDebuggerDataPointsAsync(IClientContext context, CancellationToken cancellationToken)
        {
            // This is where the code to retrieve data points from the debugger should go
            await Task.Delay(100); // Simulates an asynchronous operation

            // Example data points
            var dataPoints = new List<double> { 1.0, 2.5, 3.8, 4.2, 5.0 };

            this.logger.TraceInformation($"Retrieved data points: {string.Join(", ", dataPoints)}");

            return dataPoints;
        }
    }
}

I tried to simply put a MessageBox.Show() to determine if my Code breaks at some point and my logic is just plain wrong. But it didn’t pop up so my only guess would be that this block does not get called at all. I reverted it to the original state with the “Hello World” and it worked just fine

Leave a Reply

Your email address will not be published. Required fields are marked *