繁体   English   中英

调用 iOS Alert 时出现 UI 一致性错误

[英]UI consistency error when calling iOS Alert

我有一个 iOS Xamarin 项目,但出现以下错误:

    UIKit Consistency error: you are calling a UIKit method that 
    can only be invoked from the UI thread.

此错误发生在以下代码中:

在我的欢迎视图页面中,我有一个名为SyncButton的按钮被点击。 单击该按钮应该使用 REST 将数据与服务器同步。

在我的 WelcomeController 中,我有:

....
SyncButton.TouchUpInside += async (object sender, EventArgs e) => {
            SyncButton.Enabled = false;

            await welcomeDelegate.syncButtonCore (cred, iOSErrorAlert);

            SyncButton.Enabled = true;
        };
 ....

public void iOSErrorAlert(string LoginErrorTitle, string LoginErrorMessage){
        var Alert = UIAlertController.Create (LoginErrorTitle, LoginErrorMessage, UIAlertControllerStyle.Alert);
        Alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Cancel, null));
        PresentViewController (Alert, animated: true, completionHandler: null);

    }

当确实出现超时或其他一些错误时,应该发生警报。

SyncButtonCore()包含在如下所示的委托类中:

public async Task syncButtonCore(UserCredentials cred, RequestWithAlertDelegate.ErrorAlert NativeAlert){
        await Task.Run (async ()=>{
            RequestWithAlertDelegate requestWithAlert = new RequestWithAlertDelegate();
            string URL = URLs.BASE_URL + URLs.CASELOAD + "/" + cred.PID;
            await requestWithAlert.RequestWithRetry(URL, cred.UserID, cred.Password, NativeAlert, null, async delegate (string Response1){...}, 1);

我的RequestWithAlert类在哪里:

public async Task RequestWithRetry (string URL, string UserID, string Password, ErrorAlert NativeAlert, SyncAction Action, AsyncAction Action2, int times)
    {
        ...make sure legit credentials...
        if (LoginError) {
            NativeAlert (LoginErrorTitle, LoginErrorMessage);
        } 

在最后一段代码中,我在 NativeAlert() 函数中遇到错误的地方。 这最终抛出了在我的代码开头提到的 UIKit 错误

var Alert = UIAlertController.Create (LoginErrorTitle, LoginErrorMessage, UIAlertControllerStyle.Alert);

我不确定我在这里做错了什么,为什么我不允许创建这个警报,因为我在我的WelcomeController定义了它,我的 UIThread 应该是正确的?

问题出在我的iOSErrorAlert()函数中。 我需要用InvokeOnMainThread()包围它,因为 UI 操作应该在主线程中执行(我没有这样做,因为我将它传递给其他类/线程)。 谢谢@Paulw11 的评论。

不得不更换这个:

public void iOSErrorAlert(string LoginErrorTitle, string LoginErrorMessage){
    var Alert = UIAlertController.Create (LoginErrorTitle, LoginErrorMessage, UIAlertControllerStyle.Alert);
    Alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Cancel, null));
    PresentViewController (Alert, animated: true, completionHandler: null);
}

有了这个:

public void iOSErrorAlert(string LoginErrorTitle, string LoginErrorMessage){
    InvokeOnMainThread (() => {
        var Alert = UIAlertController.Create (LoginErrorTitle, LoginErrorMessage, UIAlertControllerStyle.Alert);
        Alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Cancel, null));
        PresentViewController (Alert, animated: true, completionHandler: null);
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM