ServiceProvider.GetService()显示错误

问题描述 投票:1回答:1

我正在尝试创建一个扩展名(.vsix),在其中我需要访问状态栏并显示自定义文本。我正在关注"Extend the status bar"正式Microsoft文档。我输入代码的方式是:

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();
}

运行此代码时,它在GetService(typeof(SVsStatusbar))此部分显示错误。

错误是:

无法从方法中推断方法'ServiceExtensions.GetService(IServiceProvider,bool)'的类型参数用法。尝试明确指定类型参数

我是用错误的方式这样做吗?我是C#的初学者。请考虑一下。谢谢!

c# visual-studio statusbar vsix vs-extensibility
1个回答
0
投票

您需要一个服务提供者实例:

System.IServiceProvider serviceProvider = package as System.IServiceProvider;
statusBar = (IVsStatusbar)serviceProvider.GetService(typeof(SVsStatusbar));
© www.soinside.com 2019 - 2024. All rights reserved.