轉(zhuǎn)帖|使用教程|編輯:龔雪|2022-09-16 10:05:46.037|閱讀 190 次
概述:本文將主要為大家介紹如何在WinForm應(yīng)用程序的時(shí)如何使用DevExpress日程控件開發(fā)相關(guān)日程視圖等,有相關(guān)實(shí)用組件推薦使用,按需下載~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在一些應(yīng)用場(chǎng)景中,我們可能需要記錄某一天,某個(gè)時(shí)段的日程安排,那么這個(gè)時(shí)候就需要引入了DevExpress的日程控件XtraScheduler了,這個(gè)控件功能非常強(qiáng)大,提供了很好的界面展現(xiàn)方式,以及很多的事件、屬性給我們定制修改,能很好滿足我們的日程計(jì)劃安排的需求。在上文中,我們?yōu)榇蠹医榻B了DevExpress日程控件(XtraScheduler)的一些表現(xiàn)效果和基本使用(點(diǎn)擊這里回顧>>),本文將繼續(xù)講解日程控件XtraScheduler的數(shù)據(jù)綁定。
DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
在日程控件里面,我們最重要,最關(guān)注的莫過于它的數(shù)據(jù)綁定及內(nèi)容顯示了,因?yàn)橹挥羞@樣,我們才可以用于實(shí)價(jià)的應(yīng)用當(dāng)中,為用戶顯示他所需的數(shù)據(jù),并存儲(chǔ)我們所需要的數(shù)據(jù)。
在日程控件里面,有相應(yīng)的引導(dǎo)我們進(jìn)行這樣的處理,還是非常不錯(cuò)的。
數(shù)據(jù)的綁定,我們需要了解日程控件的默認(rèn)處理方式,因?yàn)樗蔡峁┝艘恍?shù)據(jù)字段的信息,我們從控件的對(duì)象里面,看到有創(chuàng)建數(shù)據(jù)庫(kù)的信息,里面有一些表的字段,我們可以參考來創(chuàng)建我們的數(shù)據(jù)存儲(chǔ)信息,其中就包括了資源Resource的存儲(chǔ),日程事件安排Appointments的存儲(chǔ),如下所示。
根據(jù)這個(gè)里面的字段信息,我們可以建立自己的數(shù)據(jù)庫(kù)模型如下所示。
在數(shù)據(jù)庫(kù)里面創(chuàng)建這兩個(gè)表,并根據(jù)這些表對(duì)象,使用代碼生成工具Database2Sharp進(jìn)行代碼的快速生成,然后復(fù)制生成的代碼到具體的測(cè)試項(xiàng)目里面,生成的代碼無需任何修改即可直接使用在具體項(xiàng)目里面,測(cè)試項(xiàng)目如下代碼結(jié)構(gòu)所示。
如日程資源對(duì)象的數(shù)據(jù)庫(kù)信息,就會(huì)轉(zhuǎn)換為具體的實(shí)體類信息,供我們在界面中使用了,這樣也符合我的Winform開發(fā)框架的實(shí)體類綁定規(guī)則,提高我們數(shù)據(jù)的強(qiáng)類型約束。
如資源對(duì)象的實(shí)體類代碼生成如下所示。
/// <summary> /// 日程資源 /// </summary> [DataContract] public class AppResourceInfo : BaseEntity { /// <summary> /// 默認(rèn)構(gòu)造函數(shù)(需要初始化屬性的在此處理) /// </summary> public AppResourceInfo() { this.ID = 0; this.ResourceId = 0; this.Color = 0; this.Image = new byte[] { }; } #region Property Members [DataMember] public virtual int ID { get; set; } /// <summary> /// 資源ID /// </summary> [DataMember] public virtual int ResourceId { get; set; } /// <summary> /// 資源名稱 /// </summary> [DataMember] public virtual string ResourceName { get; set; } /// <summary> /// 顏色 /// </summary> [DataMember] public virtual int Color { get; set; } /// <summary> /// 圖形 /// </summary> [DataMember] public virtual byte[] Image { get; set; } /// <summary> /// 自定義 /// </summary> [DataMember] public virtual string CustomField1 { get; set; } #endregion }
有了這些對(duì)象,我們還需要做的就是綁定控件和保存控件數(shù)據(jù)到數(shù)據(jù)庫(kù)里面的處理。
但是這里還需要注意一個(gè)問題就是,這個(gè)日程控件數(shù)據(jù)是通過字段映射的方式進(jìn)行數(shù)據(jù)綁定的,也就是它本身也提供了幾個(gè)常規(guī)字段的信息,因此我們需要把它們的屬性和數(shù)據(jù)庫(kù)的字段(這里是實(shí)體類)的信息進(jìn)行匹配。
如我們可以通過綁定如下,事項(xiàng)Appointments和Resources的Mappings處理。
/// <summary> /// 設(shè)置日程控件的字段映射 /// </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"; }
確定控件屬性和實(shí)體類之間關(guān)系后,我們就需要從數(shù)據(jù)庫(kù)里面加載信息了。我們?cè)诖绑w的代碼里面增加兩個(gè)資源對(duì)象的集合列表,如下代碼所示。
//日程資源集合和事件列表 private List<AppResourceInfo> ResourceList = new List<AppResourceInfo>(); private List<UserAppointmentInfo> EventList = new List<UserAppointmentInfo>();
然后就是把數(shù)據(jù)從數(shù)據(jù)庫(kù)里面,通過開發(fā)框架底層的工廠類進(jìn)行數(shù)據(jù)的提取,如下代碼所示。
private void btnLoadData_Click(object sender, EventArgs e) { //從數(shù)據(jù)庫(kù)加載日程信息 List<AppResourceInfo> resouceList = BLLFactory<AppResource>.Instance.GetAll(); this.schedulerStorage1.Resources.DataSource = resouceList; List<UserAppointmentInfo> eventList = BLLFactory<UserAppointment>.Instance.GetAll(); this.schedulerStorage1.Appointments.DataSource = eventList; if (resouceList.Count > 0) { MessageDxUtil.ShowTips("數(shù)據(jù)加載成功"); } else { MessageDxUtil.ShowTips("數(shù)據(jù)庫(kù)不存在記錄"); } }
而保存數(shù)據(jù),我們把對(duì)象里面的集合存儲(chǔ)到數(shù)據(jù)庫(kù)里面即可。
private void btnSave_Click(object sender, EventArgs e) { int count = BLLFactory<AppResource>.Instance.GetRecordCount(); if (count == 0) { try { foreach (AppResourceInfo info in ResourceList) { BLLFactory<AppResource>.Instance.Insert(info); } foreach (UserAppointmentInfo info in EventList) { BLLFactory<UserAppointment>.Instance.Insert(info); } MessageDxUtil.ShowTips("數(shù)據(jù)保存成功"); } catch (Exception ex) { LogTextHelper.Error(ex); MessageDxUtil.ShowError(ex.Message); } } else { MessageDxUtil.ShowTips("數(shù)據(jù)庫(kù)已存在數(shù)據(jù)"); } }
這樣,通過代碼工具Database2Sharp生成的代碼,直接具有數(shù)據(jù)存儲(chǔ)和獲取的功能,例子就很容易明白和處理了,在實(shí)際的項(xiàng)目中,我們可能還需要存儲(chǔ)用戶的額外信息,如公司、部門、自定義信息等等,當(dāng)然也可以通過這樣的模式進(jìn)行快速的開發(fā),從而實(shí)現(xiàn)高效、統(tǒng)一、穩(wěn)定的系統(tǒng)開發(fā)過程。
但是,言歸正傳,我們前面介紹的字段,都是控件里面有的內(nèi)容,如果是控件里面沒有,我們需要增加的自定義屬性,那么我們應(yīng)該如何處理呢,還有默認(rèn)的日程界面可以修改嗎,等等這些也是我們經(jīng)常會(huì)碰到的問題。
首先我們?cè)谌粘炭丶缑嫔希ㄟ^連接按鈕的方式,創(chuàng)建一個(gè)自定義的日程窗體,如下所示:
這樣我們就可以看到,在項(xiàng)目里面增加了一個(gè)日程編輯框了,打開窗體界面,并增加一個(gè)自定義的控件內(nèi)容,最終界面如下所示。
默認(rèn)的后臺(tái)代碼里面,具有了LoadFormData和SaveFormData兩個(gè)重載的方法,這里就是留給我們對(duì)自定義屬性進(jìn)行處理的方法體了。
我們?cè)谄渲性黾硬糠肿远x屬性字段的映射處理即可,如下代碼所示。
/// <summary> /// Add your code to obtain a custom field value and fill the editor with data. /// </summary> public override void LoadFormData(DevExpress.XtraScheduler.Appointment appointment) { //加載自定義屬性 txtCustom.Text = (appointment.CustomFields["CustomField1"] == null) ? "" : appointment.CustomFields["CustomField1"].ToString(); base.LoadFormData(appointment); } /// <summary> /// Add your code to retrieve a value from the editor and set the custom appointment field. /// </summary> public override bool SaveFormData(DevExpress.XtraScheduler.Appointment appointment) { //保存自定義屬性 appointment.CustomFields["CustomField1"] = txtCustom.Text; return base.SaveFormData(appointment); }
然后我們記得在主體窗體的映射里面,為他們?cè)黾訉?duì)應(yīng)的字段映射即可,映射代碼如下所示。
AppointmentCustomFieldMappingCollection appointCust = control.Storage.Appointments.CustomFieldMappings; appointCust.Add(new AppointmentCustomFieldMapping("CustomField1","CustomField1"));
這樣就構(gòu)成了一個(gè)完整的映射信息。
/// <summary> /// 設(shè)置日程控件的字段映射 /// </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"; AppointmentCustomFieldMappingCollection appointCust = control.Storage.Appointments.CustomFieldMappings; appointCust.Add(new AppointmentCustomFieldMapping("CustomField1","CustomField1")); ResourceMappingInfo res = control.Storage.Resources.Mappings; res.Caption = "ResourceName"; res.Color = "Color"; res.Id = "ResourceId"; res.Image = "Image"; }
以上就是我在整合日程控件XtraScheduler的經(jīng)驗(yàn)總結(jié),其中已經(jīng)考慮了數(shù)據(jù)存儲(chǔ)和顯示,以及快速開發(fā)的幾個(gè)方面,當(dāng)然我們可以根據(jù)這些案例,做出更好的日程應(yīng)用來了。
本文轉(zhuǎn)載自:
DevExpress技術(shù)交流群6:600715373 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自: