如何在没有用户提示的情况下添加到日历 UWP C#

How do I add to calendar without user prompt UWP C#

我正在创建一个 Windows10 应用程序,专门用于 WindowsPhone。

该应用程序需要将约会添加到用户日历中。到目前为止,我已经设法做到了这一点,但用户必须授权每次约会。

这是我的代码(几乎直接从 GitHub 上的 UWP 示例中提取):

private async void CreateTestCalendarEntry_Click(object sender, RoutedEventArgs e)
{
    string errorMessage = null;
    var appointment = new Windows.ApplicationModel.Appointments.Appointment();

    // StartTime
    var date = DateTime.Now;
    var timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
    var startTime = new DateTimeOffset(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, timeZoneOffset);
    appointment.StartTime = startTime;

    // Subject
    appointment.Subject = "Test Calendar Entry " + date.ToString();

    if (appointment.Subject.Length > 255)
    {
        errorMessage = "The subject cannot be greater than 255 characters.";
    }

    // Location
    appointment.Location = "Japan";

    if (appointment.Location.Length > 32768)
    {
        errorMessage = "The location cannot be greater than 32,768 characters.";
    }

    // Details
    appointment.Details = "Details";

    if (appointment.Details.Length > 1073741823)
    {
        errorMessage = "The details cannot be greater than 1,073,741,823 characters.";
    }

    // Duration
    // All Day
    appointment.AllDay = true;

    // Reminder            
    appointment.Reminder = TimeSpan.FromDays(1);

    //Busy Status            
    appointment.BusyStatus = Windows.ApplicationModel.Appointments.AppointmentBusyStatus.WorkingElsewhere;

    // Sensitivity            
    appointment.Sensitivity = Windows.ApplicationModel.Appointments.AppointmentSensitivity.Public;

    var rect = new Rect(new Point(Window.Current.Bounds.Width / 2, Window.Current.Bounds.Height / 2), new Size());
    String appointmentId = await Windows.ApplicationModel.Appointments.AppointmentManager.ShowAddAppointmentAsync(appointment, rect, Windows.UI.Popups.Placement.Default);

}

使用此代码,等待行打开用户日历并显示以下内容,用户可以在其中授权或删除:

点击保存按钮,按预期执行。

问题 - 假设我有 20 个约会要添加到用户日历中,用户不会想要对每个约会进行授权。如何在不离开我的应用程序且无需用户授权的情况下创建约会?

这可以通过写入您自己的自定义日历来完成。所需的代码行如下:

async private Task CreateCalenderEntry() 
    { 
        // 1. get access to appointmentstore 
        var appointmentStore = await AppointmentManager.RequestStoreAsync(AppointmentStoreAccessType.AppCalendarsReadWrite);

        // 2. get calendar 
        var appCustomApptCalendar = await appointmentStore.CreateAppointmentCalendarAsync("MyCalendar");

        // 3. create new Appointment 
        var appo = new Windows.ApplicationModel.Appointments.Appointment();

        // appointment properties 
        appo.AllDay = true; 
        appo.Subject = "Mein Termin";            
        appo.StartTime = DateTime.Now;

        //  4. add 
        await appCustomApptCalendar.SaveAppointmentAsync(appo); 
    }

我刚刚在 Windows 10 Desktop 上试过,它成功了。应该也适用于 W10Mobile。 但是,它要求用户显示自定义日历。我认为这应该适合你。

我曾经写过一篇博文(德语)来介绍这个:https://blogs.msdn.microsoft.com/dmx/2014/07/24/termine-im-windows-phone-per-api-anlegen-ohne-broker/ 它还解释说,这甚至可以在后台运行。 (要理解文本只需使用 http://www.bing.com/translator,如果单独的代码还不够,对于给您带来的不便,我们深表歉意)。