dynamic menu creation

from:
http://community.devexpress.com/forums/t/3909.aspx

Hi forum,

I hardly try creating a menu during runtime.
Via reflection I get some info out of one assembly and based on this info I
would like to build a menu.
In my helper class where I reflect the specific assembly I create a new
subitem with some buttonitems, attached to a dummy barmanager.
This subitem is delivered to my main form where the 'real' main menu is.
I attach my submenuitem to the real barmanager and my created structure is
available. But there are no events executed.

My Snippets are quite big, sorry for that. It would be nice if someone could
give a kick into the right direction.


Best regards,

Torben Schulz



SNIP1:

MainForm.cs

DevExpress.XtraBars.BarSubItem myItem = Def.TestEnvironment.Instance.Menu;
//getting my submenuitem

this.barManager1.Items.Add (myItem);

this.bar21.LinksPersistInfo.AddRange(new
DevExpress.XtraBars.LinkPersistInfo[] {new
DevExpress.XtraBars.LinkPersistInfo(myItem)});



SNIP2:

myHelpClass:

private void LoadClassesFromAssembly()

{

if ( TestAssembly != null )

{

Type [] classesInAssembly = TestAssembly.GetTypes();

foreach (Type type in TestAssembly.GetTypes() )

{

//create subItem for each type == class

DevExpress.XtraBars.BarSubItem itemy = new DevExpress.XtraBars.BarSubItem();

itemy.Caption = type.Name;

itemy.Name = type.Name;

//add subItem to barManager

this.barManager.Items.AddRange(new DevExpress.XtraBars.BarItem[]{itemy});


//get specific methods

LoadTestMethodsFromAssembly( type , ref itemy );  //look a few lines below!!

//attach subItem to mainItem

testTopic.AddItem(itemy);

}


}

}



private void LoadTestMethodsFromAssembly( Type classType, ref
DevExpress.XtraBars.BarSubItem subItem )

{



foreach ( System.Reflection.MethodInfo methodInfo in
classType.GetMethods() )

{

if ( methodInfo.GetCustomAttributes( _testAttributeType, false ).Length >
0 )

{

DevExpress.XtraBars.BarButtonItem bbItem = new
DevExpress.XtraBars.BarButtonItem();


bbItem.Tag = new TestItemInfo( methodInfo, currentX, currentY );

bbItem.Name = methodInfo.Name;

bbItem.Caption = methodInfo.Name;

bbItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(
this.OnTestRunClicked );



subItem.AddItems ( new DevExpress.XtraBars.BarButtonItem[]{

new DevExpress.XtraBars.BarButtonItem(this.barManager,bbItem.Name)});


}

}

//This method is never executed!!!

private void OnTestRunClicked( object sender,
DevExpress.XtraBars.ItemClickEventArgs args )

{

DevExpress.XtraBars.BarButtonItem clickedItem = args.Item as
DevExpress.XtraBars.BarButtonItem;

TestItemInfo testMethod = clickedItem.Tag as TestItemInfo;

object[] coor = { testMethod.XCoordinate, testMethod.YCoordinate };

RunTest( testMethod.TestMethod.DeclaringType.ToString(),
testMethod.TestMethod.Name, coor ) ;

}

原文地址:https://www.cnblogs.com/luoyaoquan/p/2110856.html