其实XAF是比较经典的MVC(Module-View-Controller)设计。其商业逻辑部分属于Model设计,整个解决方案的通用逻辑基本都位于Module工程下,而依赖于平台的逻辑位于Module.Web或Module.Win下,所以怎么放置解决方案的各个子项都是有讲究的。用户可见的各种界面元素都属于View部分,用于呈现Module定义的各种数据模型,同样的Model可以以不同的View呈现。Controller负责用户和程序的交互,Controller是不可见的,但是它以UI元素的方式呈现给用户。
Action是属于Controller部分的,它的特点是:
1.Action是虚拟的UI元素,在特定的Action Container中显示,以UI的形式表现
2.对于某个Controller,使用Controller.Actions属性获得其Action的列表
3.ActionBase是所有Action的基类
ActionBase支持的通用行为包括了:
Event: 当用户点击一个Button,从Combox选择一项时发生。他们执行Action的实际功能
ActionBase.Executing
ActionBase.Executed
Property:
ActionBase.Active
ActionBase.Enabled
ActionBase.TargetViewType ActionBase.TargetViewNesting ActionBase.TargetViewId ActionBase.TargetObjectType 这四个属性指定action的目标视图类型,目标视图是否是顶层/嵌入的子视图,目标视图ID,目标对象的类型
ActionBase.Category 用于为Action分类,该分类是逻辑划分
ActionBase.ConfirmMessage 执行操作前的确认信息
................................................
XAF中定义了5种Action
SimpleAction 类似于普通Button
PopupWindowShowAction 类似于弹出窗口
ParametrizedAction 可接受参数的Action,例如搜索框
SingleChoiceAction 类似于下拉选框
ActionUrl 类似于超链接
怎样添加Action?
1.将action从工具栏拖入一个Controller
2.代码方式
eg.生成一个名为"MyAction"的按钮,当鼠标移过时显示“简单按钮“
[Action(ToolTip="简单按钮")]
public void MyAction(){}
各action的事件响应
ParametrizedActionprivate void MyAction_Execute(object sender, ParametrizedActionExecuteEventArgs e)
{
//获取输入的字符串
string paramValue = e.ParameterCurrentValue as string;
}
PopupWindowShowAction
//弹出窗口
private void ShowNotesAction_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e)
{
IObjectSpace objectSpace = Application.CreateObjectSpace();
e.View = Application.CreateListView(Application.FindListViewId(typeof(MyObjectClass)),
new CollectionSource(objectSpace, typeof(MyObjectClass)), true);
}
//从弹出窗口获取选择
private void ShowNotesAction_Execute(object sender, PopupWindowShowActionExecuteEventArgs e) {
} SingleChoiceAction//从弹出窗口获取信息 MyObject m=e.PopupWindow.View.SelectedObjects; //获取弹出窗体的父窗体 MyDad d=(MyDad)View.CurrentObject; View.ObjectSpace.SetModified(d); //修改MyDad的MyField字段 ViewItem i=((DetailView)View).FindItem("MyField"); //更新该字段 ((PropertyEditor)i).ReadValue(); View.ObjectSpace.CommitChanges();
private void SetTaskAction_Execute(object sender, SingleChoiceActionExecuteEventArgs e) { IObjectSpace objectSpace = View is ListView ? Application.CreateObjectSpace() : View.ObjectSpace; ArrayList objectsToProcess = new ArrayList(e.SelectedObjects); if(e.SelectedChoiceActionItem.ParentItem == setPriorityItem) { foreach(Object obj in objectsToProcess) { DemoTask objInNewObjectSpace = (DemoTask)objectSpace.GetObject(obj); objInNewObjectSpace.Priority = (Priority)e.SelectedChoiceActionItem.Data; } } else if(e.SelectedChoiceActionItem.ParentItem == setStatusItem) { foreach(Object obj in objectsToProcess) { DemoTask objInNewObjectSpace = (DemoTask)objectSpace.GetObject(obj); objInNewObjectSpace.Status = (TaskStatus)e.SelectedChoiceActionItem.Data; } } if(View is DetailView && ((DetailView)View).ViewEditMode == ViewEditMode.View) { objectSpace.CommitChanges(); } if(View is ListView) { objectSpace.CommitChanges(); View.ObjectSpace.Refresh(); } }