Smart Client Software Factory 如何使用Command

来源:互联网 发布:c php des加密解密 编辑:程序博客网 时间:2024/05/17 06:25
 SCSF中的Command体现了设计模式中的命令模式,它把事件本身和事件的逻辑代码分离开来。

下面我们在创建的View窗体之上,加上菜单(如果是ShellForm上面,应修改Infrastructure.Module工程中的ModuleController.cs),菜单的作用是,点击时在主窗体的RightWorkspace中显示一个View.

首先在Model的Constant文件夹中的CommandNames.cs中入命令名称,此名称用来标识事件

[csharp] view plaincopyprint?
  1. public class CommandNames : SmartClient.Module.Interface.Constants.CommandNames  
  2. {  
  3.     public const string ShowModelMessage = "ShowModelMessage";  
  4.     public const string ShowView1 = "ShowView1";  
  5. }  
然后修改ModelController

[csharp] view plaincopyprint?
  1. //----------------------------------------------------------------------------------------  
  2. // patterns & practices - Smart Client Software Factory - Guidance Package  
  3. //  
  4. // This file was generated by the "Add Business Module" recipe.  
  5. //  
  6. // This class contains placeholder methods for the common module initialization   
  7. // tasks, such as adding services, or user-interface element  
  8. //  
  9. //    
  10. //  
  11. //  
  12. // Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182  
  13. //----------------------------------------------------------------------------------------  
  14.   
  15. using System;  
  16. using System.Windows.Forms;  
  17. using SmartClient.Infrastructure.Interface;  
  18. using Microsoft.Practices.CompositeUI;  
  19. using Microsoft.Practices.CompositeUI.Commands;  
  20. using SmartClient.Module.Interface.Constants;  
  21.   
  22. namespace SmartClient.Module  
  23. {  
  24.     public class ModuleController : WorkItemController  
  25.     {  
  26.         public override void Run()  
  27.         {  
  28.             AddServices();  
  29.             ExtendMenu();  
  30.             ExtendToolStrip();  
  31.             AddViews();  
  32.         }  
  33.   
  34.         private void AddServices()  
  35.         {  
  36.             //TODO: add services provided by the Module. See: Add or AddNew method in   
  37.             //      WorkItem.Services collection  
  38.         }  
  39.   
  40.         private void ExtendMenu()  
  41.         {  
  42.             //TODO: add menu items here, normally by calling the "Add" method on  
  43.             //      on the WorkItem.UIExtensionSites collection.  
  44.             //        
  45.         }  
  46.   
  47.         private void ExtendToolStrip()  
  48.         {  
  49.             //TODO: add new items to the ToolStrip in the Shell. See the UIExtensionSites collection in the WorkItem.   
  50.             //        
  51.             AddToolStripButton(Constants.CommandNames.ShowModelMessage, "Hello World");  
  52.             AddToolStripButton(Constants.CommandNames.ShowView1, "Show View1");  
  53.         }  
  54.   
  55.         // This method creates a ToolStripButton and adds it to the  
  56.         // MainToolbar using the UIExtensionSites. Then it associates  
  57.         // the Click event of the button to a command.  
  58.         // UIExtensionSites are points of extension where modules can  
  59.         // add UI elements, such as items in a toolbar.  
  60.         private void AddToolStripButton(string commandName, string text)  
  61.         {  
  62.             ToolStripButton button = new ToolStripButton {Text = text, ToolTipText = text};  
  63.   
  64.             // Add the button to the MainToolBar.  
  65.             WorkItem.UIExtensionSites[UIExtensionSiteNames.MainToolbar].Add(button);  
  66.               
  67.             // Associate the Click event of the button to a command  
  68.             WorkItem.Commands[commandName].AddInvoker(button, "Click");  
  69.         }  
  70.   
  71.         private void AddViews()  
  72.         {  
  73.             //TODO: create the Module views, add them to the WorkItem and show them in   
  74.             //      a Workspace.   
  75.   
  76.             // To create and add a view you can customize the following sentence  
  77.             // SampleView view = ShowViewInWorkspace<SampleView>(WorkspaceNames.SampleWorkspace);  
  78.             View view = ShowViewInWorkspace<View>(WorkspaceNames.RightWorkspace);  
  79.         }  
  80.   
  81.         //TODO: Add CommandHandlers and/or Event Subscriptions  
  82.         //        
  83.         //        
  84.         [CommandHandler(Constants.CommandNames.ShowModelMessage)]  
  85.         public void OnShowModelMessage(object sender, EventArgs e)  
  86.         {  
  87.             // Add the HelloWorld view (smart part) to the WorkItem and  
  88.             // show the view through the RightWorkspace on the shell.  
  89.             View view = ShowViewInWorkspace<View>(WorkspaceNames.RightWorkspace);  
  90.         }  
  91.   
  92.         [CommandHandler(Constants.CommandNames.ShowView1)]  
  93.         public void OnShowView1(object sender, EventArgs e)  
  94.         {  
  95.             // Add the HelloWorld view (smart part) to the WorkItem and  
  96.             // show the view through the RightWorkspace on the shell.  
  97.             View1 view = ShowViewInWorkspace<View1>(WorkspaceNames.RightWorkspace);  
  98.             WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Disabled;  
  99.         }  
  100.     }  
  101. }  
注意这句

[csharp] view plaincopyprint?
  1. WorkItem.Commands[commandName].AddInvoker(button, "Click");  
这句作用就是把控件button的Click事件代码跟你定义的Command 联系起来,也就是说button的click事件触发的代码是打上commandName属性标签的方法,下面就是一个Command的定义

[csharp] view plaincopyprint?
  1. [CommandHandler(Constants.CommandNames.ShowView1)]  
  2. public void OnShowView1(object sender, EventArgs e)  
  3. {  
  4.     // Add the HelloWorld view (smart part) to the WorkItem and  
  5.     // show the view through the RightWorkspace on the shell.  
  6.     View1 view = ShowViewInWorkspace<View1>(WorkspaceNames.RightWorkspace);  
  7.     WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Disabled;  
  8. }  
注意这一行代码

[csharp] view plaincopyprint?
  1. WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Disabled;  
控件通常有几种状态,灰色不可用,可用,隐藏,和显示。这几点SCSF都给我们考虑到了,它们的写法分别是

[csharp] view plaincopyprint?
  1. WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Disabled;   //不可用  
  2. WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Enabled;    //可用  
  3. WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Unavailable;//不可见  
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 肚子上起红疙瘩很痒怎么办 小蚂蚁咬了肿了怎么办 锦鲤鱼尾巴烂了怎么办 泰迪身上长白毛怎么办 鱼身上有红斑了怎么办 新买锦鲤不吃食怎么办 鱼身上有红血丝怎么办 大腿内侧有红色条纹怎么办 腿上出现红血丝怎么办 孕妇有脚气,很痒怎么办 孕晚期脚气很痒怎么办 孕期有脚气很痒怎么办 激光后留下色沉怎么办 腋下很黑怎么办怎样才能变白 屁股上长一块癣怎么办 鼻两侧一热发红怎么办 脸上起皮发红痒怎么办 自癜风发红发痒怎么办 脸又干又痒怎么办 脸发红还有点痛怎么办 脸过敏了红痒怎么办 脸两边一片红痒怎么办 婴儿大腿内侧破皮怎么办 宝宝大腿根淹了怎么办 大腿一走路就疼怎么办 下面痒怎么办用什么洗 长藓怎么办用什么药膏 小腿长湿疹很痒怎么办 产后排不出大便怎么办 3岁宝宝大便不通怎么办 腿上干燥像鱼鳞怎么办 一岁脸上长癣怎么办 脖子长了一片癣怎么办 深圳摇到车牌后怎么办 发现车被套牌了怎么办 我车牌被套牌了怎么办 车子被别人套牌怎么办 车被别人套牌了怎么办 被套牌了有违章怎么办 车被套牌了怎么办报警 车牌被别人套了怎么办