C# 有没有机会;“包装”;将本机iOS API调用为async/await?

C# 有没有机会;“包装”;将本机iOS API调用为async/await?,c#,xamarin.ios,async-await,C#,Xamarin.ios,Async Await,考虑以下iOS 6 API调用: eventStore.RequestAccess(EKEntityType.Event, (granted, error) => { if(granted) { events = this.GetLocalCalendarEvents(eventStore); }}); 我有一些访问日历和读取事件的代码。在iOS5上,这只是工作,但在iOS6上,我必须先请求访问,如果允许,然后开始阅读。 我想知道我是否可以以某种方式将其包装为async/awai

考虑以下iOS 6 API调用:

eventStore.RequestAccess(EKEntityType.Event, (granted, error) => {
if(granted)
{
    events = this.GetLocalCalendarEvents(eventStore);
}});
我有一些访问日历和读取事件的代码。在iOS5上,这只是工作,但在iOS6上,我必须先请求访问,如果允许,然后开始阅读。 我想知道我是否可以以某种方式将其包装为async/await的组合,以隐藏丑陋的委托


有什么想法吗?

您可以使用
TaskCompletionSource

public Task<bool> DoSomethingAsync()
{
   var taskSource = new TaskCompletionSource<bool>();

   //Call some asynchronous Apple API
   NSSomeAppleApi.DoSomething(error =>
   {
      if (error != null)
         taskSource.SetException(new Exception(error.LocalizationDescription));
      else
         taskSource.SetResult(true);
   });

   return taskSource.Task;
}
公共任务DoSomethingAsync()
{
var taskSource=new TaskCompletionSource();
//调用一些异步的appleapi
NSSomeAppleApi.DoSomething(错误=>
{
if(错误!=null)
SetException(新异常(error.LocalizationDescription));
其他的
taskSource.SetResult(true);
});
返回taskSource.Task;
}

不错!!我不知道TaskCompletionSource()!