2017-08-15 211 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