【问题标题】:Getting Parameters for my Addin获取我的插件的参数
【发布时间】:2014-03-13 13:59:32
【问题描述】:

我已经编写了一个 VS 2012 插件,现在我想获取打开插件时点击的文件。

这是 OnConnection 方法:

public void OnConnection(object application, ext_ConnectMode connectMode, object   addInInst, ref Array custom)
{
  _applicationObject = (DTE2)application;
  _addInInstance = (AddIn)addInInst;
  if (connectMode == ext_ConnectMode.ext_cm_UISetup)
  {
    object[] contextGUIDS = new object[] { };
    Commands2 commands = (Commands2)_applicationObject.Commands;
    Microsoft.VisualStudio.CommandBars.CommandBar standardToolBar =
      ((Microsoft.VisualStudio.CommandBars.CommandBars)
      _applicationObject.CommandBars)["Reference Item"];

    try
    {
      //Add a command to the Commands collection:
      Command command = commands.AddNamedCommand2(
        _addInInstance,
        "ProxyClassCreatorAddin",
        "ProxyClassCreatorAddin",
        "Executes the command for ProxyClassCreatorAddin",
        true,
        2677,
        ref contextGUIDS,
        (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
        (int)vsCommandStyle.vsCommandStylePictAndText,
        vsCommandControlType.vsCommandControlTypeButton);

      //Add a control for the command to the tools menu:
      if ((command != null) && (standardToolBar != null))
      {
        CommandBarControl ctrl =
              (CommandBarControl)command.AddControl(standardToolBar, 1);
        ctrl.TooltipText = "Executes the command for MyAddin";
      }
    }
    catch (System.ArgumentException)
    {
      //If we are here, then the exception is probably because a command with that name
      //  already exists. If so there is no need to recreate the command and we can 
      //  safely ignore the exception.
    }
  }
}

所以用户在选定的引用上单击鼠标右键,然后我的插件启动,但 varInExec 方法)为空,我如何获取选定引用的文件/文件名/路径?

编辑:VSIX 是不可能的

【问题讨论】:

  • 考虑制作 VSIX 扩展而不是插件。

标签: c# visual-studio-2012 visual-studio-addins


【解决方案1】:

我在 exec 方法中找到了答案。诀窍是获取 UIHierachy(在我的项目中是 SolutionExplorer)并获取 selectedItems。这样,我的程序就使用了所有选中项中找到的第一个项目。

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
  handled = false;
  if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
  {
    if (commandName == "ProxyClassCreatorAddin.Connect.ProxyClassCreatorAddin")
    {
      string filePath = string.Empty;
      UIHierarchy uih = _applicationObject.ToolWindows.SolutionExplorer;
      Array selectedItems = (Array)uih.SelectedItems;
      if (selectedItems != null)
      {
        foreach (UIHierarchyItem item in selectedItems)
        {
          var x = item.Object.GetType();
          Project projectItem = item.Object as Project;
          filePath = projectItem.Properties.Item("FullPath").Value.ToString();
        }
      }

      handled = true;
      CreateProxyClasses.CreateProxyClasses form = new CreateProxyClasses.CreateProxyClasses(filePath);
      form.ShowDialog();
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    相关资源
    最近更新 更多