Tanmay Bairagi
Tanmay Bairagi

Reputation: 624

ServiceProvider.GetService() showing error

I am trying to create an extension (.vsix) in which I need to access status bar and show custom text. I am following "Extend the status bar" official Microsoft document. The way I am putting the code is :

private Command1(AsyncPackage package, OleMenuCommandService commandService)
{
     this.package = package ?? throw new ArgumentNullException(nameof(package));
     commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));

     var menuCommandID = new CommandID(CommandSet, CommandId);
     var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);//calling spot
     commandService.AddCommand(menuItem);
}
private void MenuItemCallback(object sender, EventArgs e)
{
    //Error Line
    IVsStatusbar statusBar = (IVsStatusbar)ServiceProvider.GetService(typeof(SVsStatusbar));
    //Error Line

    // Make sure the status bar is not frozen
    int frozen;

    statusBar.IsFrozen(out frozen);

    if (frozen != 0)
    {
        statusBar.FreezeOutput(0);
    }

    // Set the status bar text and make its display static.
    statusBar.SetText("We just wrote to the status bar.");

    // Freeze the status bar.
    statusBar.FreezeOutput(1);

    // Get the status bar text.
    string text;
    statusBar.GetText(out text);
    System.Windows.Forms.MessageBox.Show(text);

    // Clear the status bar text.
    statusBar.FreezeOutput(0);
    statusBar.Clear();
}

While running this code it is showing error on GetService(typeof(SVsStatusbar)) this part.

The error is :

The type arguments for method 'ServiceExtensions.GetService(IServiceProvider, bool)' cannot be inferred from the usage. Try specifying the type arguments explicitly

Am I doing this in the wrong way? I am beginner for C#. Please consider this. Thanks!

Upvotes: 2

Views: 1233

Answers (1)

Sergey Vlasov
Sergey Vlasov

Reputation: 27930

You need a service provider instance:

System.IServiceProvider serviceProvider = package as System.IServiceProvider;
statusBar = (IVsStatusbar)serviceProvider.GetService(typeof(SVsStatusbar));

Upvotes: 2

Related Questions