Windows 10 Phone 跨平台项目日历

Windows 10 Phone Calendar on Cross Platform Project

我正在尝试将设备日历集成到我的 Xamarin Forms 应用程序中,但是,在 Windows 10 Mobile 中面临一些严重的问题!在 UWP 的平台特定项目上,我调用以下原生 API:

var store = await Windows.ApplicationModel.Appointments.AppointmentManager.RequestStoreAsync(Windows.ApplicationModel.Appointments.AppointmentStoreAccessType.AppCalendarsReadWrite);

调用 API 后,平台特定项目抛出此异常:

消息过滤器表明应用程序正忙。 对 ASTA(线程 9840)的 COM 调用(IID:{638BB2DB-451D-4661-B099-414F34FFB9F1},方法索引:6)出现死锁并超时。

估计跟多线程和应用领域有关系吧!考虑到我已将日历权限授予该应用程序,以前有人遇到过类似的错误吗?

我的开发环境

我检查了你的项目并重现了这个问题。您可以通过将 .Result 替换为 await 关键字来解决问题。

Although async programming is relatively straightforward, there are some details to keep in mind which can prevent unexpected behavior.

async methods need to have an await keyword in their body or they will never yield!

This is important to keep in mind. If await is not used in the body of an async method, the C# compiler will generate a warning, but the code will compile and run as if it were a normal method. Note that this would also be incredibly inefficient, as the state machine generated by the C# compiler for the async method would not be accomplishing anything.

详情请参考Asynchronous programming

不建议在属性的get方法中做更多的逻辑代码。所以你可以在 OnAppearing 方法中得到你的 CalendarEvents 就像下面的代码一样。

protected async override void OnAppearing()
{
    base.OnAppearing();
    var events = await DependencyService.Get<ICalendar>(DependencyFetchTarget.GlobalInstance).GetEventsAsync();
}