SCSF中的Command体现了设计模式中的命令模式,它把事件本身和事件的逻辑代码分离开来。
下面我们在创建的View窗体之上,加上菜单(如果是ShellForm上面,应修改Infrastructure.Module工程中的ModuleController.cs),菜单的作用是,点击时在主窗体的RightWorkspace中显示一个View.
首先在Model的Constant文件夹中的CommandNames.cs中入命令名称,此名称用来标识事件
public class CommandNames : SmartClient.Module.Interface.Constants.CommandNames { public const string ShowModelMessage = "ShowModelMessage"; public const string ShowView1 = "ShowView1"; }
然后修改ModelController
//---------------------------------------------------------------------------------------- // patterns & practices - Smart Client Software Factory - Guidance Package // // This file was generated by the "Add Business Module" recipe. // // This class contains placeholder methods for the common module initialization // tasks, such as adding services, or user-interface element // // // // // Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182 //---------------------------------------------------------------------------------------- using System; using System.Windows.Forms; using SmartClient.Infrastructure.Interface; using Microsoft.Practices.CompositeUI; using Microsoft.Practices.CompositeUI.Commands; using SmartClient.Module.Interface.Constants; namespace SmartClient.Module { public class ModuleController : WorkItemController { public override void Run() { AddServices(); ExtendMenu(); ExtendToolStrip(); AddViews(); } private void AddServices() { //TODO: add services provided by the Module. See: Add or AddNew method in // WorkItem.Services collection } private void ExtendMenu() { //TODO: add menu items here, normally by calling the "Add" method on // on the WorkItem.UIExtensionSites collection. // } private void ExtendToolStrip() { //TODO: add new items to the ToolStrip in the Shell. See the UIExtensionSites collection in the WorkItem. // AddToolStripButton(Constants.CommandNames.ShowModelMessage, "Hello World"); AddToolStripButton(Constants.CommandNames.ShowView1, "Show View1"); } // This method creates a ToolStripButton and adds it to the // MainToolbar using the UIExtensionSites. Then it associates // the Click event of the button to a command. // UIExtensionSites are points of extension where modules can // add UI elements, such as items in a toolbar. private void AddToolStripButton(string commandName, string text) { ToolStripButton button = new ToolStripButton {Text = text, ToolTipText = text}; // Add the button to the MainToolBar. WorkItem.UIExtensionSites[UIExtensionSiteNames.MainToolbar].Add(button); // Associate the Click event of the button to a command WorkItem.Commands[commandName].AddInvoker(button, "Click"); } private void AddViews() { //TODO: create the Module views, add them to the WorkItem and show them in // a Workspace. // To create and add a view you can customize the following sentence // SampleView view = ShowViewInWorkspace<SampleView>(WorkspaceNames.SampleWorkspace); View view = ShowViewInWorkspace<View>(WorkspaceNames.RightWorkspace); } //TODO: Add CommandHandlers and/or Event Subscriptions // // [CommandHandler(Constants.CommandNames.ShowModelMessage)] public void OnShowModelMessage(object sender, EventArgs e) { // Add the HelloWorld view (smart part) to the WorkItem and // show the view through the RightWorkspace on the shell. View view = ShowViewInWorkspace<View>(WorkspaceNames.RightWorkspace); } [CommandHandler(Constants.CommandNames.ShowView1)] public void OnShowView1(object sender, EventArgs e) { // Add the HelloWorld view (smart part) to the WorkItem and // show the view through the RightWorkspace on the shell. View1 view = ShowViewInWorkspace<View1>(WorkspaceNames.RightWorkspace); WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Disabled; } } }
注意这句
WorkItem.Commands[commandName].AddInvoker(button, "Click");
这句作用就是把控件button的Click事件代码跟你定义的Command 联系起来,也就是说button的click事件触发的代码是打上commandName属性标签的方法,下面就是一个Command的定义
[CommandHandler(Constants.CommandNames.ShowView1)] public void OnShowView1(object sender, EventArgs e) { // Add the HelloWorld view (smart part) to the WorkItem and // show the view through the RightWorkspace on the shell. View1 view = ShowViewInWorkspace<View1>(WorkspaceNames.RightWorkspace); WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Disabled; }
注意这一行代码
WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Disabled;
控件通常有几种状态,灰色不可用,可用,隐藏,和显示。这几点SCSF都给我们考虑到了,它们的写法分别是
WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Disabled; //不可用 WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Enabled; //可用 WorkItem.Commands[Constants.CommandNames.ShowView1].Status = CommandStatus.Unavailable;//不可见