简体   繁体   English

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

[英]UI consistency error when calling iOS Alert

I have an iOS Xamarin project were I am getting the following error:我有一个 iOS Xamarin 项目,但出现以下错误:

    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:此错误发生在以下代码中:

In my Welcome view page I have a button called SyncButton that gets clicked.在我的欢迎视图页面中,我有一个名为SyncButton的按钮被点击。 This click of the button is supposed to sync data with a server using REST.单击该按钮应该使用 REST 将数据与服务器同步。

In my WelcomeController I have:在我的 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);

    }

Alerts should happen when there is a timeout or some other error really.当确实出现超时或其他一些错误时,应该发生警报。

The SyncButtonCore() is contained in a delegate class that looks like this: 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);

Where my RequestWithAlert class is:我的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);
        } 

Where I am getting my error at the NativeAlert() function in that last bit of code.在最后一段代码中,我在 NativeAlert() 函数中遇到错误的地方。 Which ultimately throws the UIKit error mentioned at the beginning in my code at这最终抛出了在我的代码开头提到的 UIKit 错误

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

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

Problem was in my iOSErrorAlert() function.问题出在我的iOSErrorAlert()函数中。 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).我需要用InvokeOnMainThread()包围它,因为 UI 操作应该在主线程中执行(我没有这样做,因为我将它传递给其他类/线程)。 Thank you @Paulw11 for the comment.谢谢@Paulw11 的评论。

Had to replace this:不得不更换这个:

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

With this:有了这个:

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