无法在未调用Looper.prepare()[xamarin]

问题描述:

的线程中创建处理程序所以我实际上没有问题,因为我已经解决了它,但万一有别人遇到此问题,它总是很高兴有一个整洁的解决方案。无法在未调用Looper.prepare()[xamarin]

虽然有大量的"Can't create handler inside thread which has not called Looper.prepare()" - 问题没有标签xamarin。 (所以他们是所有Java和我有0次比赛为“无法里面()[xamarin]线程已经不叫Looper.prepare创建处理程序”)

public static class PageExtensions 
{ 
    public static Task<bool> DisplayAlertOnUi(this Page source, string title, string message, string accept, string cancel) 
    { 
     TaskCompletionSource<bool> doneSource = new TaskCompletionSource<bool>(); 
     Device.BeginInvokeOnMainThread(async() => 
     { 
      try 
      { 
       var result = await source.DisplayAlert(title, message, accept, cancel); 
       doneSource.SetResult(result); 
      } 
      catch (Exception ex) 
      { 
       doneSource.SetException(ex); 
      } 
     }); 

     return doneSource.Task; 
    } 
} 

最后我有一个情况下使用TaskCompletionSource到解决问题。