调用 iOS Alert 时出现 UI 一致性错误 [英] UI consistency error when calling iOS Alert

查看:12
本文介绍了调用 iOS Alert 时出现 UI 一致性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an iOS Xamarin project were I am getting the following error:

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

出现此错误的代码如下:

This error occurs with the following code:

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

In my Welcome view page I have a button called SyncButton that gets clicked. This click of the button is supposed to sync data with a server using REST.

在我的 WelcomeController 中,我有:

In my WelcomeController I have:

....
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);

    }

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

Alerts should happen when there is a timeout or some other error really.

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

The SyncButtonCore() is contained in a delegate class that looks like this:

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 错误

Where I am getting my error at the NativeAlert() function in that last bit of code. Which ultimately throws the UIKit error mentioned at the beginning in my code at

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

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

I'm not sure what I am doing wrong here and why I am not allowed to create this alert because I define it in my WelcomeController which is where my UIThread should be correct?

推荐答案

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

Problem was in my iOSErrorAlert() function. I needed to surround it with the InvokeOnMainThread() because UI actions should be performed in the main thread (which I was not doing because I was passing it around to other classes/threads). Thank you @Paulw11 for the comment.

不得不替换这个:

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);
    });
}

这篇关于调用 iOS Alert 时出现 UI 一致性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆