在上篇隨筆《在Winform開發中使用日程控件XtraScheduler》中介紹了DevExpress的XtraScheduler日程控件的各種使用知識點,對於我們來說,日程控件不陌生,如OutLook里面就有日歷的模塊,但是這個日程控件真的是很復雜的一個控件,需要全面掌握可能需要花費很多的時間去了解,由於是技術研究,我總是希望把它常用的功能剖析的更加徹底一些,前面隨筆也介紹了它的存儲功能,把它基於實體類的方式存儲在數據庫里面,不過介紹的還不夠,本文繼續上面的內容,進行數據存儲方面的介紹。
在查閱了大量資料,以及一兩天的潛入研究,總算把它的數據存儲和相關熟悉有一個比較清晰的了解。
1、數據綁定及加載的處理回顧
在上篇隨筆里面,我總體性介紹了這個控件的數據綁定,以及數據是如何保存到數據庫里面的,綁定到DevExpress的XtraScheduler日程控件的步驟是需要先設置好映射關系(Mappings),然后綁定數據源即可。
操作代碼如下所示。
/// <summary> /// 設置日程控件的字段映射 /// </summary> /// <param name="control">日程控件</param> private void SetMappings(SchedulerControl control) { AppointmentMappingInfo appoint = control.Storage.Appointments.Mappings; appoint.AllDay = "AllDay"; appoint.Description = "Description"; appoint.End = "EndDate"; appoint.Label = "AppLabel"; appoint.Location = "Location"; appoint.RecurrenceInfo = "RecurrenceInfo"; appoint.ReminderInfo = "ReminderInfo"; appoint.ResourceId = "ResourceId"; appoint.Start = "StartDate"; appoint.Status = "Status"; appoint.Subject = "Subject"; appoint.Type = "EventType"; ResourceMappingInfo res = control.Storage.Resources.Mappings; res.Caption = "ResourceName"; res.Color = "Color"; res.Id = "ResourceId"; res.Image = "Image"; }
然后接着就是綁定Appointment和Resource到對應的數據源里面接口。
//從數據庫加載日程信息 List<AppResourceInfo> resouceList = BLLFactory<AppResource>.Instance.GetAll(); this.schedulerStorage1.Resources.DataSource = resouceList; List<UserAppointmentInfo> eventList = BLLFactory<UserAppointment>.Instance.GetAll(); this.schedulerStorage1.Appointments.DataSource = eventList;
2、日程數據的增刪改處理
但是,上面這樣的存儲在是實際上是比較少的,也就是我們往往可能會在界面上進行新增或者復制記錄,修改記錄,或者刪除記錄等操作,因此需要進一步利用日程控件的完善接口來處理這些操作。
我們在VS的對應控件屬性里面可以看到一些關於存儲的重要事件,也就是日程的增刪改處理事件,如下所示。
上面這幾個事件也就是對應在日程控件里面右鍵菜單對應的增刪改操作。
另外日程控件還可以支持拖動修改、拖動復制、刪除鍵刪除操作的,這些也是會繼續調用上面那些增刪改的操作事件的,所以我們就對他們進行完善,我們重點是處理ing類型的事件,如Inserting的事件,在寫入日程控件集合之前的處理。
//寫回數據庫操作的事件 control.Storage.AppointmentInserting += Storage_AppointmentInserting; control.Storage.AppointmentChanging += Storage_AppointmentChanging; control.Storage.AppointmentDeleting += Storage_AppointmentDeleting;
對於修改數據前的處理,我們是讓它在順利寫入數據庫后,在決定是否更新日程對象的存儲集合還是丟棄修改記錄,如下所示。
void Storage_AppointmentChanging(object sender, PersistentObjectCancelEventArgs e) { Appointment apt = e.Object as Appointment; UserAppointmentInfo info = ConvertToAppoint(apt); bool success = BLLFactory<UserAppointment>.Instance.Update(info, apt.Id); e.Cancel = !success; }
注意上面的e.Cancel =true或者false代表是否放棄,上面的代碼邏輯就是如果我們順利寫入數據庫,那么就可以成功更新到日程控件的存儲集合里面,而且就可以在界面看到最新的結果。
有了上面的理解,我們就可以進一步完善在插入前、刪除前的代碼處理了。
對於刪除前的操作,我們可以用的代碼如下所示。
void Storage_AppointmentDeleting(object sender, PersistentObjectCancelEventArgs e) { Appointment apt = e.Object as Appointment; if (apt != null && apt.Id != null) { if (MessageDxUtil.ShowYesNoAndWarning("您確認要刪除該記錄嗎?") == DialogResult.Yes) { bool success = BLLFactory<UserAppointment>.Instance.Delete(apt.Id); e.Cancel = !success; } } }
我們使用代碼MessageDxUtil.ShowYesNoAndWarning來判斷是否繼續,如下界面所示。
對於插入的記錄,我們需要更加注意,需要寫入數據庫后,進行本地的存儲記錄的更新,這樣才能合理顯示,否則容易發生復制、創建的記錄位置總是不對,偏移到其他地方去的。
void Storage_AppointmentInserting(object sender, PersistentObjectCancelEventArgs e) { Appointment apt = e.Object as Appointment; UserAppointmentInfo info = ConvertToAppoint(apt); bool success = BLLFactory<UserAppointment>.Instance.Insert(info); e.Cancel = !success; if (success) { LoadData(); } }
LoadData就是我們從數據庫加載日程信息,並綁定到日程控件的存儲對象里面,其中需要注意的就是需要使用RefreshData方法,讓日程控件的存儲對象刷新一下,這樣才能夠順利顯示我們添加的記錄。
//從數據庫加載日程信息 List<AppResourceInfo> resouceList = BLLFactory<AppResource>.Instance.GetAll(); this.schedulerStorage1.Resources.DataSource = resouceList; List<UserAppointmentInfo> eventList = BLLFactory<UserAppointment>.Instance.GetAll(); this.schedulerStorage1.Appointments.DataSource = eventList;
this.schedulerControl1.RefreshData();//必須,每次修改需要刷新數據源,否則界面需要重新刷新
3、多人資源的處理
在日程控件里面,支持多人資源的處理,默認是資源只能選擇其一,需要多人的話,那么就需要設置下面的屬性來顯示聲明使用多人資源,如下所示。
schedulerControl1.Storage.Appointments.ResourceSharing = true;
使用多人的資源,可以對資源進行復選,它的映射記錄就是ResourceIds的了,所以設置映射屬性的時候,我們需要判斷這個ResourceSharing 屬性。
if(control.ResourceSharing) { appoint.ResourceId = "ResourceIds"; } else { appoint.ResourceId = "ResourceId"; }
其中ResourceId的內容格式如下所示
<ResourceIds> <ResourceId Type="System.String" Value="1" /><ResourceId Type="System.String" Value="2" /> </ResourceIds>
和ResourceId不同這里的值就是一個XML內容,這個和提醒等內容的存儲格式一樣,都是基於XML的內容。日程控件涉及到的幾種XML的信息獲取如下所示。
//多人資源的信息 if(apt.ResourceIds != null) { AppointmentResourceIdCollectionContextElement item = new AppointmentResourceIdCollectionContextElement(apt.ResourceIds); info.ResourceIds = item.ValueToString(); //第二種 //AppointmentResourceIdCollectionXmlPersistenceHelper helper = new AppointmentResourceIdCollectionXmlPersistenceHelper(apt.ResourceIds); //info.ResourceIds = helper.ToXml(); } //日程重復信息 if (apt.RecurrenceInfo != null) { info.RecurrenceInfo = apt.RecurrenceInfo.ToXml(); } //提醒信息 if (apt.Reminder != null) { info.ReminderInfo = ReminderCollectionXmlPersistenceHelper.CreateSaveInstance(apt).ToXml(); }
以上就是我們經常用到的日程控件的處理內容了,希望對大家有所幫助。