在使用XAF应用程序时,可能经常需要从控制器或者活动中访问业务对象。例如,在实现操作时,可能需要访问一个集中的对象,以便在执行操作时修改其属性值。
注:官方项目源码详见参考网址。
在执行操作时访问当前选定的对象
无论操作类型如何,传递给事件的参数都包括SimpleActionExecuteEventArgs.CurrentObject和SimpleActionExecuteEventArgs.SelectedObjects属性。CurrentObject属性,指定当前的活动对象;SelectedObjects是在活动视图中选择的对象的集合。
以下代码针对联系人进行操作(点击事件)。该操作执行时,它将向选中的对象(列表视图(ListView)中选中的对象或者详细视图(DetailView)所对应的对象)Note添加一行显示信息。
1 using System; 2 using System.Collections; 3 using DevExpress.ExpressApp; 4 using DevExpress.ExpressApp.Actions; 5 using DevExpress.ExpressApp.EF; 6 //... 7 public partial class MyNotesController : ViewController { 8 public MyNotesController() { 9 SimpleAction myAction = new SimpleAction(this, "Salary Info", "Edit"); 10 myAction.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects; 11 myAction.TargetObjectType = typeof(Contact); 12 myAction.Execute += myAction_Execute; 13 Actions.Add(myAction); 14 } 15 void myAction_Execute(object sender, SimpleActionExecuteEventArgs e) { 16 ArrayList SelectedContacts = new ArrayList(); 17 if ((e.SelectedObjects.Count > 0) && 18 ((e.SelectedObjects[0] is XafDataViewRecord) || (e.SelectedObjects[0] is XafInstantFeedbackRecord))) { 19 foreach (var selectedObject in e.SelectedObjects) { 20 SelectedContacts.Add((Contact)ObjectSpace.GetObject(selectedObject)); 21 } 22 } 23 else { 24 SelectedContacts = (ArrayList)e.SelectedObjects; 25 } 26 foreach (Contact selectedContact in SelectedContacts) { 27 DateTime now = DateTime.Now; 28 selectedContact.Notes += " [INFO] Your salary is transfered " + 29 now.ToString("M/d/yy") + " at " + now.ToString("hh:mm"); 30 } 31 ObjectSpace.CommitChanges(); 32 ObjectSpace.Refresh(); 33 } 34 }
使用视图控制器访问当前选定的对象
一种不太常见的任务是,从控制器访问视图的焦点和选定对象。在本例中,使用ViewController.View属性指定的视图对象的View.CurrentObject和View.SelectedObjects属性。CurrentObject属性,指定当前的活动对象;SelectedObjects是在活动视图中选择的对象的集合。
下面的代码片段演示了一个用于联系人详细信息视图的控制器。如果您要删除一个联系人,那么将被删除的联系人的全名将添加到confirm消息中。如果您想删除多个联系人,则将添加所选联系人计数。
1 public class MyConfirmationController : ViewController { 2 private string defaultMessage; 3 DeleteObjectsViewController deleteObjectsViewController; 4 public MyConfirmationController() { 5 this.TargetObjectType = typeof(Contact); 6 } 7 protected override void OnActivated() { 8 base.OnActivated(); 9 deleteObjectsViewController = Frame.GetController<DeleteObjectsViewController>(); 10 if (deleteObjectsViewController != null) { 11 defaultMessage = deleteObjectsViewController.DeleteAction.GetFormattedConfirmationMessage(); 12 View.SelectionChanged += View_SelectionChanged; 13 UpdateConfirmationMessage(); 14 } 15 } 16 void View_SelectionChanged(object sender, System.EventArgs e) { 17 UpdateConfirmationMessage(); 18 } 19 private void UpdateConfirmationMessage() { 20 if (View.SelectedObjects.Count == 1) { 21 deleteObjectsViewController.DeleteAction.ConfirmationMessage = 22 String.Format("You are about to delete the '{0}' Contact. Do you want to proceed?", 23 ((Contact)View.CurrentObject).FullName); 24 } 25 else { 26 deleteObjectsViewController.DeleteAction.ConfirmationMessage = 27 String.Format("You are about to delete {0} Contacts. Do you want to proceed?", 28 (View.SelectedObjects.Count)); 29 } 30 } 31 protected override void OnDeactivated() { 32 base.OnDeactivated(); 33 if (deleteObjectsViewController != null) { 34 View.SelectionChanged -= View_SelectionChanged; 35 deleteObjectsViewController.DeleteAction.ConfirmationMessage = defaultMessage; 36 deleteObjectsViewController = null; 37 } 38 } 39 }
注:当视图在详细视图(DataView)模式运行时,View.CurrentObject 和 View.SelectedObjects 返回的是XafDataViewRecord对象,而不是原始业务对象。要获得原始的对象,使用View.ObjectSpace.GetObject(obj)方法
参考网址
[2]官方项目源码:http://www.devexpress.com/example=E3016