UWP约会:任命ID返回空

问题描述:

我正在尝试将Appointments添加到我的UWP应用程序。UWP约会:任命ID返回空

我已成功设置约会,但创建约会ID的结果为空,这表示约会未创建。

以下是我的代码:

 public static Rect GetElementRect(FrameworkElement element) 
     { 
      GeneralTransform transform = element.TransformToVisual(null); 
      Point point = transform.TransformPoint(new Point()); 
      return new Rect(point, new Size(element.ActualWidth, element.ActualHeight)); 
     } 
     private async Task<ManagerResponseModel> AddAppointmentToOutlookCalendar(ViewingSummaryModel model) 
     { 
      ManagerResponseModel result = new ManagerResponseModel(); 

      //Add appointment if assigned to the same user 
      if(model.HousingOfficerId == AppSession.LoggedinUserId) 
      { 
       // Create an Appointment that should be added the user's appointments provider app. 
       var appointment = new Appointment(); 
       //Populate Viewing Data in appointment 
       appointment.Subject = string.Format("Viewing at {0}", model.PropertyAddress); 
       appointment.Location = (string.IsNullOrWhiteSpace(model.PropertyAddress)) ? "NA" : model.PropertyAddress; 
       appointment.BusyStatus = AppointmentBusyStatus.Tentative; 
       appointment.Sensitivity = AppointmentSensitivity.Public; 
       appointment.AllDay = false; 
       //var timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now); 
       var date = GeneralHelper.GetCombinedDateTimeStringForViewing(model.ViewingDate, model.FormattedTime); 
       //var startTime = new DateTimeOffset(date.Year, date.Month, date.Day, date.Hour, date.Minute, 0, TimeZoneInfo.Local.BaseUtcOffset); 
       appointment.StartTime = date; 
       appointment.Details = string.Format("Customer: {0}", model.CustomerName) + "\r" 
        + string.Format("Housing Officer: {0}", (string.IsNullOrWhiteSpace(model.AssignedTo)) ? "NA" : model.AssignedTo) + "\r" 
        + string.Format("Address: {0}", model.PropertyAddress) + "\r" 
        + string.Format("Created by: {0}", (string.IsNullOrWhiteSpace(model.CreatorName)) ? "You" : model.CreatorName); 




       // Get the selection rect of the button pressed to add this appointment 
       var rect = GetElementRect(this.Frame as FrameworkElement); 
       string appointmentId = string.Empty; 
       // ShowAddAppointmentAsync returns an appointment id if the appointment given was added to the user's calendar. 
       // This value should be stored in app data and roamed so that the appointment can be replaced or removed in the future. 
       // An empty string return value indicates that the user canceled the operation before the appointment was added. 

       if (!string.IsNullOrWhiteSpace(model.OutlookIdentifier)) 
       { 
        appointmentId = await AppointmentManager.ShowReplaceAppointmentAsync(model.OutlookIdentifier, appointment, rect, Placement.Default, date); 
        /*Appointment doesn't exist on this system, try to add a new one*/ 
        if (string.IsNullOrWhiteSpace(appointmentId)) 
        { 
         appointmentId = await AppointmentManager.ShowAddAppointmentAsync(appointment, rect, Windows.UI.Popups.Placement.Default); 
        } 
       } 
       else 
       { 
        appointmentId = await AppointmentManager.ShowAddAppointmentAsync(appointment, rect, Windows.UI.Popups.Placement.Default); 
       } 

       model.OutlookIdentifier = appointmentId; 

       result.isSuccessful = string.IsNullOrWhiteSpace(appointmentId); 
       result.responseObject = model; 
      } 
      else 
      { 
       result.isSuccessful = true; 
       result.responseObject = model; 
      } 

      return result; 
     } 

以下行:

appointmentId = await AppointmentManager.ShowAddAppointmentAsync(appointment, rect, Windows.UI.Popups.Placement.Default); 

返回空只有当用户取消该操作或有导致失败的创作任命的其他一些问题。我没有取消手术,也没有任何异常,所以我不知道我在这里做错了什么。

在阅读相关thread on MSDN后,我想出了它。这是一个非常愚蠢的错误。我忘记了在我的Package.appxmanifest文件中添加约会capability

所以问题是,我的应用程序未被授权将约会添加到用户的日历,这就是约会ID返回空的原因(如果相关错误还被返回,微软也会很好)。

为了解决这个问题,下面的行添加到您的package.appxmanifest文件功能:

<Capabilities> 
    <uap:Capability Name="appointments" /> 
</Capabilities> 

或者,你可以直接点击该文件,进入功能选项卡,选中想在“约会”的能力下面的截图:

enter image description here

+1

您应该勾选此作为回答。 ;) –

+1

@JustinXL不能明天:3 – NSNoob