在 vsix 包中使用 DynamicItemStart 时如何更新 Visual Studio UI [英] how to update Visual Studio UI when using DynamicItemStart inside a vsix package

查看:26
本文介绍了在 vsix 包中使用 DynamicItemStart 时如何更新 Visual Studio UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在菜单控制器中实现一个 DynamicItemStart 按钮.当 Visual Studio 启动时,我正在加载此按钮的动态项目.一切都已正确加载,因此调用 initialize 方法并在此动态按钮中看到所有新项目.包完全加载后,我想向此动态按钮添加更多项目,但由于包已加载,因此不会再次调用 initialize 方法,因此我无法在此动态按钮中看到新项目.我只看到VS启动时加载的那些.

I'm implementing a DynamicItemStart button inside a Menu Controller. I'm loading the dynamic items for this button when Visual Studio starts. Everything is loaded correctly so the initialize method is called an I see all the new items in this Dynamic button. After the package is completely loaded I want to add more items to this Dynamic button, but since the package is already loaded the initialize method is not called again and I cannot see the new items in this Dynamic button. I only see the ones that were loaded when VS started.

有什么办法可以强制更新这个动态按钮,让它显示新项目?.我希望能够在添加更多项目但在 Initialize 方法之外更新 VS UI.我所做的实现与此 msdn 示例中显示的实现非常相似:

Is there any way that I can force the update of this Dynamic button so it shows the new items?. I want to be able to update the VS UI after I added more items but outside the Initialize method. The implementation I did is very similar to the one showed on this msdn example:

http://msdn.microsoft.com/en-us/library/bb166492.aspx

有谁知道UI的更新是否可以按需完成?

Does anyone know if an Update of the UI can be done by demand?

非常感谢任何提示.

推荐答案

我终于搞定了.主要是实现了一个派生类的 OleMenuCommand,它实现了一个带有 Predicate 的新构造函数.此谓词用于检查新命令是否与 DynamicItemStart 按钮匹配.

I finally got this working. The main thing is the implementation of a derived class of OleMenuCommand that implements a new constructor with a Predicate. This predicate is used to check if a new command is a match within the DynamicItemStart button.

public class DynamicItemMenuCommand : OleMenuCommand
{
private Predicate<int> matches;
public DynamicItemMenuCommand(CommandID rootId, Predicate<int> matches, EventHandler invokeHandler, EventHandler beforeQueryStatusHandler)
  : base(invokeHandler, null, beforeQueryStatusHandler, rootId)
{
  if (matches == null)
  {
    throw new ArgumentNullException("Matches predicate cannot be null.");
  }

  this.matches = matches;
}


public override bool DynamicItemMatch(int cmdId)
{
  if (this.matches(cmdId))
  {
    this.MatchedCommandId = cmdId;
    return true;
  }

  this.MatchedCommandId = 0;
  return false;      
}

}

在执行时添加命令时应使用上述类.这是创建命令的代码

The above class should be used when adding the commands on execution time. Here's the code that creates the commands

public class ListMenu
{
 private int _baselistID = (int)PkgCmdIDList.cmdidMRUList;    
 private List<IVsDataExplorerConnection> _connectionsList;


public ListMenu(ref OleMenuCommandService mcs)
{             
  InitMRUMenu(ref mcs);
}    

internal void InitMRUMenu(ref OleMenuCommandService mcs)
{            
  if (mcs != null)
  {               
    //_baselistID has the guid value of the DynamicStartItem 
    CommandID dynamicItemRootId = new CommandID(GuidList.guidIDEToolbarCmdSet, _baselistID);
    DynamicItemMenuCommand dynamicMenuCommand = new DynamicItemMenuCommand(dynamicItemRootId,      isValidDynamicItem, OnInvokedDynamicItem, OnBeforeQueryStatusDynamicItem);
          mcs.AddCommand(dynamicMenuCommand);                  
  }
}

private bool IsValidDynamicItem(int commandId)    
{      
  return ((commandId - _baselistID) < connectionsCount);  // here is the place to put the criteria to add a new command to the dynamic button
}


private void OnInvokedDynamicItem(object sender, EventArgs args)
{
  DynamicItemMenuCommand invokedCommand = (DynamicItemMenuCommand)sender;      

  if (null != invokedCommand)
  {
     .....
  }
}

private void OnBeforeQueryStatusDynamicItem(object sender, EventArgs args)
{
  DynamicItemMenuCommand matchedCommand = (DynamicItemMenuCommand)sender;           

  bool isRootItem = (matchedCommand.MatchedCommandId == 0);
    matchedCommand.Enabled = true;
    matchedCommand.Visible = true;
    int indexForDisplay = (isRootItem ? 0 : (matchedCommand.MatchedCommandId - _baselistID));
    matchedCommand.Text = "Text for the command";
  matchedCommand.MatchedCommandId = 0;
}  

}

我不得不查看大量文档,因为不太清楚如何在执行时添加命令.所以我希望这可以节省一些时间,无论谁必须实现类似的东西.

I had to review a lot of documentation since it was not very clear how the commands can be added on execution time. So I hope this save some time whoever has to implement anything similar.

这篇关于在 vsix 包中使用 DynamicItemStart 时如何更新 Visual Studio UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆