gpt4 book ai didi

c# - ui 工具包中的错误一致性错误

转载 作者:行者123 更新时间:2023-11-30 13:12:49 30 4
gpt4 key购买 nike

我在访问 View Controller .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.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.

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

最佳答案

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

解决方案很简单:仅从 UI 线程修改 UI。这实质上意味着,如果您使用的是前面带有“UI”的类,则您应该从 UI 线程进行操作。 (这是一个经验法则,还有其他时间在 UI 线程上)。

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

  • 当在 NSObject 的子类中时,InvokeOnMainThread 将起到作用。
  • 在任何地方,CoreFoundation.DispatchQueue.MainQueue.DispatchAsync 都将始终有效。

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

所以在您的代码中,如果我们添加一个 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");
});
}

关于c# - ui 工具包中的错误一致性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39120789/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com