How to update Visual Studio interface when using DynamicItemStart inside vsix package

I am implementing a DynamicItemStart button inside a menu controller. I load dynamic elements for this button when starting Visual Studio. Everything loads correctly, so the initialize method is called I see all the new elements in this dynamic button. After the package is fully loaded, I want to add additional elements to this Dynamic button, but since the package is already loaded, the initialize method is not called again, and I do not see new elements in this dynamic button. I see only those that were loaded when VS started.

Is there any way to forcibly update this Dynamic button to display new items ?. I want to be able to update the VS UI after adding a few elements, but outside the Initialize method. The implementation I made is very similar to the one shown in this msdn example:

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

Does anyone know if updating a user interface can fulfill a request?

Any advice is appreciated.

+1
source share
2 answers

I finally got this job. The main thing is the implementation of the OleMenuCommand derived class, which implements the new constructor with Predicate. This predicate is used to check the correspondence of a new command in 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;      
}

}

. ,

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

}

, , . , , - .

+2

, .

, , ( IsValidDynamicItem ) , - true, OnBeforeQueryStatusDynamicItem (Enabled/Visible/Checked/Text ..) , .

+1

All Articles