使用Xamarin.Forms跨平台C#设置设备日历提醒

8

使用Xamarin.Forms跨平台C#设置设备日历提醒,可以通过使用XamForms.Controls.Calendar插件添加事件和显示日历,但现在我想在Xamarin Forms中使用跨平台方式设置特定设备日历的提醒。

1个回答

10

在Xamarin.Forms项目中,无法直接使用/访问日历,您需要使用DependancyService以及为每个平台编写代码。我附上了参考代码。

Windows //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);
}

Android http://developer.xamarin.com/guides/android/user_interface/calendar/ - 同时使用了Android文档以更好地理解变量

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 可移植类库中使用它 - sonu
我已经提到过,您需要使用DependencyService。 - Chandresh Khambhayata
@ChandreshKhambhayata 在这个函数中,你是从纪元时间计算毫秒时间吗? - Parth Patel
@ChandreshKhambhayata 然后在保存该事件之后,是否有任何方法可以删除该事件? - Parth Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接