답변

5

DependancyService을 사용해야하고 각 플랫폼에 대한 코드를 작성해야하는 경우 Xamarin.Forms 프로젝트에서 직접 캘린더를 사용하거나 액세스 할 수 없습니다. 참조 용 코드를 첨부합니다.

윈도우 // 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/ - 또한 IOS // http://developer.xamarin.com/guides/ios/platform_features/introduction_to_event_kit/

변수

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; 
} 

의 추가 이해를 위해 안드로이드 문서 도구를 사용

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; 
    } 
+0

xamarin potable 클래스 라이브러리에서 사용하는 방법 – sonu

+0

DependencyService –

+0

@ChandreshKhambhayata GetDateTimeMS()를 사용해야한다고 언급 한 적이 있습니다.이 함수에서이 시간에 밀리 초 단위로 시간을 계산하고 있습니까 ?? –

관련 문제