使用 xamarin.forms 跨平台 C# 在设备日历上设置提醒

Posted

技术标签:

【中文标题】使用 xamarin.forms 跨平台 C# 在设备日历上设置提醒【英文标题】:Set reminder on device calendar using xamarin.forms cross platform C# 【发布时间】:2017-07-10 15:58:45 【问题描述】:

使用 xamarin.forms 跨平台 c# 在设备日历上设置提醒,使用 XamForms.Controls.Calendar 插件添加事件和日历显示,但现在我想使用跨平台在 xamarin 表单中设置特定设备日历上的提醒

【问题讨论】:

【参考方案1】:

您不能直接从 Xamarin.Forms 项目使用/访问日历,因为您需要使用 DependancyService 并且需要为每个平台编写代码。我附上代码供参考。

窗户 //https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn495339.aspx

public async Task AddAppointment(ESF.Core.Models.ESFPortal_Events appointment)

    var appointmentRcd = new Windows.ApplicationModel.Appointments.Appointment();
    var date = appointment.ExpireDate.Value.Date;
    var time = appointment.ExpireDate.Value.TimeOfDay;
    var timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
    var startTime = new DateTimeOffset(date.Year, date.Month, date.Day, time.Hours, time.Minutes, 0, timeZoneOffset);
    appointmentRcd.StartTime = startTime;

    // Subject
    appointmentRcd.Subject = appointment.Title;
    // Location
    appointmentRcd.Location = appointment.WhereWhen;
    // Details
    appointmentRcd.Details = appointment.Description;
    // Duration          
    appointmentRcd.Duration = TimeSpan.FromHours(1);
    // All Day
    appointmentRcd.AllDay = false;
    //Busy Status
    appointmentRcd.BusyStatus = Windows.ApplicationModel.Appointments.AppointmentBusyStatus.Busy;
    // Sensitivity
    appointmentRcd.Sensitivity = Windows.ApplicationModel.Appointments.AppointmentSensitivity.Public;
    Rect rect = new Rect(new Point(10, 10), new Size(100, 200));
string retVal = await AppointmentManager.ShowAddAppointmentAsync(appointmentRcd, rect, Windows.UI.Popups.Placement.Default);
    return !string.IsNullOrEmpty(retVal);

安卓 //http://developer.xamarin.com/guides/android/user_interface/calendar/ - 还使用了 Android Docs 来额外理解变量

public async Task<bool> AddAppointment(ESF.Core.Models.ESFPortal_Events appointment)

    Intent intent = new Intent(Intent.ActionInsert);
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.Title, appointment.Title);
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.Description, appointment.WhereWhen + " " + appointment.Description);
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.Dtstart, GetDateTimeMS(appointment.ExpireDate.Value));
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.Dtend, GetDateTimeMS(appointment.ExpireDate.Value.AddHours(1)));
    intent.PutExtra(CalendarContract.ExtraEventBeginTime, GetDateTimeMS(appointment.ExpireDate.Value));
    intent.PutExtra(CalendarContract.ExtraEventEndTime , GetDateTimeMS(appointment.ExpireDate.Value.AddHours(1)));
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.EventTimezone, "UTC");
    intent.PutExtra(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "UTC");
    intent.SetData(CalendarContract.Events.ContentUri);
    ((Activity)Forms.Context).StartActivity(intent);
    return true;

ios //http://developer.xamarin.com/guides/ios/platform_features/introduction_to_event_kit/

protected EKEventStore eventStore;
public AppointmentServiceh_iOS()

    eventStore = new EKEventStore();

public async Task<bool> AddAppointment(ESF.Core.Models.ESFPortal_Events appointment)

    var granted = await eventStore.RequestAccessAsync(EKEntityType.Event);//, (bool granted, NSError e) =>
    if (granted.Item1)
    
        EKEvent newEvent = EKEvent.FromStore(eventStore);
        newEvent.StartDate = DateTimeToNSDate(appointment.ExpireDate.Value);
        newEvent.EndDate = DateTimeToNSDate(appointment.ExpireDate.Value.AddHours(1));
        newEvent.Title = appointment.Title;
        newEvent.Notes = appointment.WhereWhen;
        newEvent.Calendar = eventStore.DefaultCalendarForNewEvents;
        NSError e;
        eventStore.SaveEvent(newEvent, EKSpan.ThisEvent, out e);
        return true;
    
    else
    
        new UIAlertView("Access Denied", "User Denied Access to Calendar Data", null, "ok", null).Show();
        return false;
    

public DateTime NSDateToDateTime(NSDate date)

    double secs = date.SecondsSinceReferenceDate;
        if (secs < -63113904000)
            return DateTime.MinValue;
        if (secs > 252423993599)
            return DateTime.MaxValue;
        return (DateTime)date;
    

    public NSDate DateTimeToNSDate(DateTime date)
    
        if (date.Kind == DateTimeKind.Unspecified)
            date = DateTime.SpecifyKind(date, DateTimeKind.Local);
        return (NSDate)date;
    

【讨论】:

如何在 xamarin 便携类库中使用它 我已经提到你需要使用DependencyService @ChandreshKhambhayata GetDateTimeMS() 在此函数中,您正在计算从纪元时间开始的毫秒时间?? @ChandreshKhambhayata 保存该事件后有没有办法删除该事件?

以上是关于使用 xamarin.forms 跨平台 C# 在设备日历上设置提醒的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin.Forms for iOS初体验

Xamarin.Forms for iOS初体验

从零開始学Xamarin.Forms 概述

使用 Xamarin.Forms(xaml 和 c#)创建超链接

Xamarin.Forms自定义用户界面控件实现一个HybridWebView(混合webview)

在 xamarin.forms 中使用 c# 的长按检测器