SmartClient Software factory中的Composite UI Application Block(Cab)技术了解(四):Command

Command描述和实例

       在系统开发中常遇到对控件的事件委托问题,某个事件需要在容器内进行共享,那就需要通过Command的应用来实现。

1.         创建一个CommandModule

2.5.1

       创建一个XML文件准备把需要添加的MenuItem信息放进去:

<Maps>

  <list Item="FileItem" Label="One" Name="ClickOne"/>

  <list Item="FileItem" Label="Two" Name="ClickTwo"/>

  <list Item="FileItem" Label="Three" Name="ClickThree"/>

  <list Item="FileItem" Label="Four" Name="ClickFour"/>

</Maps>

     然后创建对应的类ToolListItemToolListItemMap用于添加MenuItem的控制类,这样,前期的工作已经弄好,然后就要处理如何把这些ITEM和对应的触发事件放到Module里了。

2.       ModuleController中添加以下代码用于MenuItem的添加和事件的绑定:

private void ExtendMenu()

        {

            ToolListItemMap map = new ToolListItemMap();

 

            foreach (ToolListItem item in map.MenuListMap)

            {

                ToolStripMenuItem menuitem = new ToolStripMenuItem(item.Label);

                menuitem.Name = item.Name;

 

                if (WorkItem.UIExtensionSites.Contains("FileItem"))

                {

                    UIExtensionSite site = WorkItem.UIExtensionSites["FileItem"];

                    site.Add(menuitem);

                    WorkItem.Commands[item.CommandName].AddInvoker(menuitem, "Click");

                }

            }

 

        }   

        #region 定义COMMAND事件

        [CommandHandler(CommandNames.OneCmd)]

        public void oneCmd(object sender, EventArgs e)

        {

            MessageBox.Show("show one!");

 

        }

        [CommandHandler(CommandNames.TwoCmd)]

        public void twoCmd(object sender, EventArgs e)

        {

            MessageBox.Show("show two!");

        }

        [CommandHandler(CommandNames.ThreeCmd)]

        public void threeCmd(object sender, EventArgs e)

        {

            MessageBox.Show("show three!");

        }

        [CommandHandler(CommandNames.FourCmd)]

        public void fourCmd(object sender, EventArgs e)

        {

            MessageBox.Show("show four!");

        }

        #endregion

其中WorkItem.Commands[item.CommandName].AddInvoker(menuitem, "Click");这句是把事件添加到容器的关键。

3.       保存后运行程序显示为图2.5.2和图2.5.3所示。

2.5.2

2.5.3

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章