2017-06-15 30 views
2

我正在嘗試將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); 

返回空只有當用戶取消該操作或有導致失敗的創作任命的其他一些問題。我沒有取消手術,也沒有任何異常,所以我不知道我在這裏做錯了什麼。

回答

2

在閱讀相關thread on MSDN後,我想出了它。這是一個非常愚蠢的錯誤。我忘記了在我的Package.appxmanifest文件中添加約會capability

所以問題是,我的應用程序未被授權將約會添加到用戶的日曆,這就是約會ID返回空的原因(如果相關錯誤還被返回,微軟也會很好)。

爲了解決這個問題,下面的行添加到您的package.appxmanifest文件功能:

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

或者,你可以直接點擊該文件,進入功能選項卡,選中想在「約會」的能力下面的截圖:

enter image description here

+1

您應該勾選此作爲回答。 ;) –

+1

@JustinXL不能明天:3 – NSNoob