繁体   English   中英

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

[英]Set reminder on device calendar using xamarin.forms cross platform C#

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

您不能直接从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 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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM