本文介绍了ui kit 中的错误一致性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a issue accesing a text box in a view controller .cs file

 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 方法上的文本框

It is within this funciton that it complains it cannot access a text box which is on a aynsc method

    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.UIKitThreadAccessException: UIKit Consistency error: you are调用只能从 UI 线程调用的 UIKit 方法.

即使密码正确,我的密码正确也不会显示.我是否必须在单独的线程上运行 UI 警报?

Also my Password Correct does not show even though if it is a correct password.Do i have to run the UI Alerts on a seperate thread?

推荐答案

任何 UIKit 方法都必须从 UI 线程(或主线程、主队列等)调用.这确保了 UI 的一致性.Xamarin 在调试模式下添加对所有 UIKit 方法的检查,并在您尝试使用后台线程更改 UI 时引发该异常.

Any UIKit methods must be called from the UI thread (or Main thread, Main queue, etc.). This ensures consistency in the UI. Xamarin adds a check to all UIKit methods in debug mode and throws that exception if you try to use a background thread to change the UI.

解决办法很简单:只从UI线程修改UI.这实质上意味着如果您使用的类前面带有UI",则您可能应该从 UI 线程中执行此操作.(这是一个经验法则,还有其他时间需要在 UI 线程上进行).

The solution is simple: only modify the UI from the UI thread. That essentially means if you're using a class with "UI" in front it, you should probably do it from the UI thread. (That's a rule of thumb and there are other times to be on the UI thread).

如何在这个神话般的 UI 线程上获取我的代码?我很高兴你问.在 iOS 中,您有几个选择:

How do I get my code on this mythical UI thread? I'm glad you asked. In iOS, you have a few options:

  • 当在 NSObject 的子类中时,InvokeOnMainThread 会起作用.
  • 在任何地方,CoreFoundation.DispatchQueue.MainQueue.DispatchAsync 将始终有效.
  • When in a subclass of NSObject, InvokeOnMainThread will do the trick.
  • From anywhere, CoreFoundation.DispatchQueue.MainQueue.DispatchAsync will always work.

这两个方法都只接受一个 Action,它可以是 lambda 或方法.

Both of those methods just accept an Action, which can be a lambda or a method.

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

So in your code, if we add an InvokeOnMainThread (because I think this is in your UIViewController subclass)...

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

这篇关于ui kit 中的错误一致性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:56