ui kit一致性错误

本文关键字:错误 一致性 kit ui | 更新日期: 2023-09-27 18:03:48

我在访问视图控制器。cs文件中的文本框时遇到问题

 async partial void loginUser(UIButton sender)
{
    // Show the progressBar as the MainActivity is being loade

    Console.WriteLine("Entered email : " + txtEmail.Text);

        // Create user object from entered email
        mCurrentUser = mJsonHandler.DeserialiseUser(txtEmail.Text);
        try
        {
            Console.WriteLine("Starting network check");
            // Calls email check to see if a registered email address has been entered
            if (EmailCheck(txtEmail.Text) == true)
            {
                await  CheckPassword();
            }
            else
            {
                UIAlertView alert = new UIAlertView()
                {
                    Title = "Login Alert",
                    Message = "Incorrect email or password entered"
                };
                alert.AddButton("OK");
                alert.Show();
            }

        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("An error has occured: '{0}'", ex);
        }

在这个函数中,它抱怨不能访问aynsc方法上的文本框

    public Task CheckPassword()
    {
   return Task.Run(() =>
    {
            // Creates instance of password hash to compare plain text and encrypted passwords.
            PasswordHash hash = new PasswordHash();
            // Checks password with registered user password to confirm access to account.
            if (hash.ValidatePassword(txtPassword.Text ,mCurrentUser.password)==true)
            {
                Console.WriteLine("Password correct");

                UIAlertView alert = new UIAlertView()
                {
                    Title = "Login Alert",
                    Message = "Password Correct Loggin In"
                };
                alert.AddButton("OK");
                alert.Show();
                //insert intent call to successful login here.
            }
            else
            {
                UIAlertView alert = new UIAlertView()
                {
                    Title = "Login Alert",
                    Message = "Incorrect email or password entered"
                };
                alert.AddButton("OK");
                alert.Show();
            }
            Console.WriteLine("Finished check password");
        });
    }

在这一行出现错误:

txtPassword.Text 

错误如下:

UIKit。UIKit一致性错误:你是调用一个只能从UI线程调用的UIKit方法。

也我的密码正确不显示,即使它是一个正确的密码。我必须在一个单独的线程上运行UI警报吗?

ui kit一致性错误

任何UIKit方法必须从UI线程(或主线程,主线队列等)调用。这确保了UI的一致性。Xamarin在调试模式下为所有UIKit方法添加了一个检查,如果你试图使用后台线程来改变UI,就会抛出异常。

解决方案很简单:只从UI线程修改UI。那本质上意味着如果你使用一个在它前面有"UI"的类,你可能应该从UI线程做它。(这是一个经验法则,还有其他时间需要在UI线程上)。

我如何在这个神秘的UI线程上得到我的代码?我很高兴你这么问。在iOS中,你有几个选项:

  • 当在NSObject的子类中时,InvokeOnMainThread将完成这个任务。
  • CoreFoundation.DispatchQueue.MainQueue.DispatchAsync在任何地方都能正常工作。

这两种方法都只接受一个Action,可以是lambda,也可以是method。

所以在你的代码中,如果我们添加一个InvokeOnMainThread(因为我认为这是在你的UIViewController子类)…

 public Task CheckPassword()
 {
     return Task.Run(() =>
     {
        // Creates instance of password hash to compare plain text and encrypted passwords.
        PasswordHash hash = new PasswordHash();
        // Checks password with registered user password to confirm access to account.
        InvokeOnMainThread(() => {
            if (hash.ValidatePassword(txtPassword.Text ,mCurrentUser.password)==true)
            {
                Console.WriteLine("Password correct");

                UIAlertView alert = new UIAlertView()
                {
                    Title = "Login Alert",
                    Message = "Password Correct Loggin In"
                };
                alert.AddButton("OK");
                alert.Show();
                //insert intent call to successful login here.
            }
            else
            {
                UIAlertView alert = new UIAlertView()
                {
                    Title = "Login Alert",
                    Message = "Incorrect email or password entered"
                };
                alert.AddButton("OK");
                alert.Show();
            }
        });
        Console.WriteLine("Finished check password");
    });
}

也许这对某人有帮助。所以我要加上解决我的问题的方法,这和rogue是一样的。在iOS中使用xamarin表单时,请遵循避免这种一致性错误的代码

 await Task.Run(async () =>
            {
                await Device.InvokeOnMainThreadAsync(async () =>
                {
                    await MaterialDialog.Instance.SnackbarAsync(message: "Bla bla bla",
                                           msDuration: MaterialSnackbar.DurationShort).ConfigureAwait(false);
                }).ConfigureAwait(false);
            }).ConfigureAwait(false);