为我的插件获取参数

时间:2014-03-13 13:59:33

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

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

这是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不可能

1 个答案:

答案 0 :(得分:0)

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