2017-08-15 147 views
0

在我的Xamarin iOS应用程序中,我有一个显示警告对话框的静态辅助方法。如果用户仍未按下“确定”按钮,此对话框需要几秒钟后自动消失。下面是简化的代码片段:UIAlertController没有被解雇

UIAlertController dlg = UIAlertController.Create(title, text, 
    UIAlertControllerStyle.Alert); 
dlg.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Cancel, null); 
UIApplication.SharedApplication.KeyWindow.RootViewController. 
    PresentViewController(dlg, true, null); 

后来,当计时器到期:

t.Elapsed += (s, e) => { 
    dlg.DismissViewController(true, null); 
}; 

虽然,该方法DismissViewController确实得到调用时,对话框不会从屏幕上消失。

我甚至试过打电话dlg.Dispose(),但这也没有帮助。

有人能帮我理解我失踪的是什么吗?问候。

回答

3

事件代码已过期不在主线程中。

在主线程上调用它。

t.Elapsed += (s, e) => 
{ 
    InvokeOnMainThread(() => { 
     dlg.DismissViewController(true, null); 
    }); 
}; 
+0

完美。谢谢。 – Peter