DevExpress Winforms使用技巧教程:Scheduler - 與Google日歷同步
下載DevExpress v20.1完整版 DevExpress v20.1漢化資源獲取
DevExpress Winforms Controls 內置140多個UI控件和庫,完美構建流暢、美觀且易于使用的應用程序。想要體驗?點擊下載>>
從v18.2版本開始,DevExpress Winforms可以使用Google日歷進行新的Scheduler同步,本文主要為大家介紹此功能。
DXGoogleCalendarSync組件
要同步Scheduler和Google Calendars Appointments(在Google文檔中,這些稱為Events),您需要一個DXGoogleCalendarSync組件。 單擊Add DX Google Calendar Synchronizer Smart Tag鏈接時,Scheduler會自動添加它,該組件需要幾個NuGet軟件包。

DXGoogleCalendarSync在Scheduler存儲(支持SchedulerDataStorage及其先前的SchedulerStorage)與選定的Google日歷之間傳輸數據,可以將存儲同時綁定到任何受支持的數據源。

您不僅限于導入或導出數據,同步是一個雙向過程,可同時處理兩個任務。
局限性
DXGoogleCalendarSync組件可以正確識別和同步Scheduler支持的所有Appointment類型:常規Appointment和定期Appointment、all-day events、假期等,該組件唯一無法自動同步的內容類型是顏色信息:Appointment標簽和狀態不轉換默認為事件顏色。狀態是唯一的Appointment屬性,沒有匹配的事件屬性(Google事件僅提供兩個“statuses” - Busy和Free),您可以通過處理某些組件事件來手動將標簽與事件顏色同步(請參見此)。
第二個限制是DXGoogleCalendarSync一次只能使用一個日歷,而Google日歷用戶可以預訂多個不同的日歷:個人日歷、工作日歷、假期日歷、家庭日歷、同事之間共享的公司日歷等,當然,您可以輕松地檢索日歷列表,并實現一個UI在運行時在它們之間進行切換。
技術細節
顯示運行中的同步組件。
要管理Google Calendar Events,您需要訪問Google Calendar API。 請單擊Google文檔中,然后按照步驟13b下載您自己的credentials.json文件,這應該包含在您的項目中,GitHub示例對此文件使用名稱。
組件上需要設置三個核心屬性:
- Storage - 存儲Appointments的Scheduler Storage對象。 在示例中,此屬性是在設計時分配的()
gcSyncComponent.Storage = schedulerStorage;
-
CalendarService - 允許組件訪問和修改日歷,必須授權當前用戶訪問Google帳戶(這是示例方法)。
... GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, new String[] { CalendarService.Scope.Calendar }, "user", CancellationToken.None, new FileDataStore(credPath, true));
然后可以實例化CalendarService(在示例的中)。
... gcSyncComponent.CalendarService = new CalendarService(new BaseClientService.Initializer() { HttpClientInitializer = this.credential, ApplicationName = "GoogleCalendarSyncSample" }); ...
-
CalendarID - 同步GoogleCalendar identifier,這是一些示例代碼,可檢索所有Google日歷并填充組合框。然后您可以處理SelectedIndexChanged組合框事件,來將有效ID分配給CalendarID屬性。
CalendarList calendarList; string activeCalendarId; async Task UpdateCalendarListUI() { var listRequest = this.service.CalendarList.List(); this.calendarList = await listRequest.ExecuteAsync(); this.ricbCalendarList.Items.Clear(); foreach (CalendarListEntry item in this.calendarList.Items) this.ricbCalendarList.Items.Add(item.Summary); if (!String.IsNullOrEmpty(this.activeCalendarId)) { var itemToSelect = this.calendarList.Items.FirstOrDefault( x => x.Id == this.activeCalendarId); this.gcSyncComponent.CalendarId = this.activeCalendarId; if (this.ricbCalendarList.Items.Contains(itemToSelect.Summary)) this.beiCalendarList.EditValue = itemToSelect.Summary; else this.activeCalendarId = String.Empty; } }
一切設置完成后,可以隨時在DXGoogleCalendarSync組件上調用Synchronize或SynchronizeAsync來觸發同步進程。首次啟動時,默認的網絡瀏覽器將顯示一個頁面,要求您登錄要用于同步的Google帳戶。
除了上述基本設置外,DXGoogleCalendarSync組件還提供其他功能,包括:
- 會話狀態日志記錄,來幫助確定Scheduler Appointments和Google Events的優先級
- 發生次要同步沖突時,將觸發一個專用事件,處理它來手動確定有效性
- 自定義字段,用于存儲Google事件ID和eTag值
- ProgressChanged事件,用于跟蹤自定義進度指示器的同步狀態
有關Google日歷同步功能的完整文檔,請訪問。
中國區首發 · DevExpress v20.1新版發布會報名開啟,名額有限先到先得哦~
DevExpress技術交流群2:775869749 歡迎一起進群討論