Winforms界面开发之如何与Google日历同步

下载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软件包。

DevExpress Winforms使用技巧教程

DXGoogleCalendarSync在Scheduler存储(支持SchedulerDataStorage及其先前的SchedulerStorage)与选定的Google日历之间传输数据,可以将存储同时绑定到任何受支持的数据源。

DevExpress Winforms使用技巧教程

您不仅限于导入或导出数据,同步是一个双向过程,可同时处理两个任务。

局限性

DXGoogleCalendarSync组件可以正确识别和同步Scheduler支持的所有Appointment类型:常规Appointment和定期Appointment、all-day events、假期等,该组件唯一无法自动同步的内容类型是颜色信息:Appointment标签和状态不转换默认为事件颜色。状态是唯一的Appointment属性,没有匹配的事件属性(Google事件仅提供两个“statuses” - Busy和Free),您可以通过处理某些组件事件来手动将标签与事件颜色同步(请参见此GitHub example)。

第二个限制是DXGoogleCalendarSync一次只能使用一个日历,而Google日历用户可以预订多个不同的日历:个人日历、工作日历、假期日历、家庭日历、同事之间共享的公司日历等,当然,您可以轻松地检索日历列表,并实现一个UI在运行时在它们之间进行切换。

技术细节

该GitHub示例显示运行中的同步组件。

要管理Google Calendar Events,您需要访问Google Calendar API。 请单击Google文档中Quickstart页面的此链接,然后按照步骤13b下载您自己的credentials.json文件,这应该包含在您的项目中,GitHub示例对此文件使用名称client_secret.json

组件上需要设置三个核心属性:

  • Storage - 存储Appointments的Scheduler Storage对象。 在示例中,此属性是在设计时分配的(请参见此处

gcSyncComponent.Storage = schedulerStorage;

  • CalendarService - Google Calendar Service允许组件访问和修改日历,必须授权当前用户访问Google帐户(这是示例方法AuthorizeToGoogle)。
    ...
    GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(stream).Secrets,
    new String[] { CalendarService.Scope.Calendar },
    "user",
    CancellationToken.None,
    new FileDataStore(credPath, true));

然后可以实例化CalendarService(在示例的OnLoad中)。

...
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技术交流群2:775869749      欢迎一起进群讨论

慧都高端UI界面开发
原文地址:https://www.cnblogs.com/AABBbaby/p/13185843.html