【问题标题】:ServiceProvider.GetService() showing errorServiceProvider.GetService() 显示错误
【发布时间】:2019-12-19 10:17:38
【问题描述】:

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

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】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2014-02-13
      • 2012-08-11
      • 2013-03-07
      • 2017-05-26
      • 2014-04-09
      • 2016-05-08
      • 2018-01-11
      • 1970-01-01
      相关资源
      最近更新 更多