UWP约会:约会ID返回空

时间:2017-06-15 13:45:13

标签: c# uwp

我正在尝试将Appointments添加到我的UWP应用中。

我已成功设置约会,但创建的约会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);
仅当用户取消操作或存在导致创建约会失败的其他问题时,

才返回空。我没有取消操作,也没有任何提出的异常,所以我不知道我在这里做错了什么。

1 个答案:

答案 0 :(得分:2)

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

所以问题是,我的应用程序没有被授权向用户的日历添加约会,这就是为什么约会ID返回为空(如果相关错误也被返回,那将是很好的,微软)。

要解决此问题,请在功能:

中将以下行添加到package.appxmanifest文件中
<Capabilities>
    <uap:Capability Name="appointments" />
</Capabilities>

或者,您只需点击该文件,转到功能选项卡,然后查看“约会”功能,如下面的屏幕截图所示:

enter image description here