【发布时间】:2017-03-13 20:30:06
【问题描述】:
我正在通过 xamarin 开发一个应用程序,并且我正在使用一个按钮来检查注册字段。
Button sendToRegisterButton = (Button)FindViewById(Resource.Id.registerbtn);
sendToRegisterButton.Touch += delegate { CheckFields(); };
因此,当按钮被触摸时,我会检查布尔值touched,以查看按钮是否已被按下。
如果if(!touched) 通过,我将touch 设置为true。我让代码运行,并在 if 语句结束时再次设置为 false。现在,当我对此进行测试并按下手机上的注册按钮时,仍会多次调用 checkfields 函数。
相关代码:
private void CheckFields()
{
if (!touched)
{
touched = true;
//Rest of code here... checking the username and password fields etc
touched = false;
}
}
编辑:我尝试禁用按钮并在之后重新启用它,不幸的是没有工作
private void CheckFields()
{
sendToRegisterButton.Enabled = false;
//Rest of code here... checking the username and password fields etc
sendToRegisterButton.Enabled = true;
}
EDIT2:既然人们在问,这就是整个班级
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using ArcheologieApplication.Code;
using System.Text.RegularExpressions;
using ArcheologieApplication.Code.Queries;
namespace ArcheologieApplication
{
[Activity(Label = "ArcheologieApplication", MainLauncher = true, Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen")]
class RegisterActivity : Activity
{
private Button sendToRegisterButton;
PlayerInfoQuery playerQuery = new PlayerInfoQuery();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.RegisterLayout);
SetupButtons();
}
private void SetupButtons()
{
Button loginButton = (Button)FindViewById(Resource.Id.signinbtn);
loginButton.Touch += delegate { StartActivity(typeof(LoginActivity)); };
sendToRegisterButton = (Button)FindViewById(Resource.Id.registerbtn);
sendToRegisterButton.Touch += delegate { CheckFields(); };
}
private void CheckFields()
{
sendToRegisterButton.Enabled = false;
RegexUtilities checkMail = new RegexUtilities();
var username = FindViewById<EditText>(Resource.Id.regnametxt);
var userEmail = FindViewById<EditText>(Resource.Id.regemailtxt);
var userPassword = FindViewById<EditText>(Resource.Id.regpasstxt);
var userPasswordConfirm = FindViewById<EditText>(Resource.Id.regconfirmtxt);
PlayerInfo player = new PlayerInfo();
bool nameBool = false;
bool emailBool = false;
bool passBool = false;
if (string.IsNullOrEmpty(username.Text))
{
Toast.MakeText(this, "Invalid Username", ToastLength.Short).Show();
}
else
{
player.PlayerName = username.Text;
nameBool = true;
}
if (string.IsNullOrEmpty(userEmail.Text))
{
//TODO: Verify Email adress for valid email.
Toast.MakeText(this, "Invalid Email Adress", ToastLength.Short).Show();
}
else
{
if (checkMail.IsValidEmail(userEmail.Text))
{
player.PlayerEmail = userEmail.Text;
emailBool = true;
}
else
{
Toast.MakeText(this, "Invalid Email Adress", ToastLength.Short).Show();
}
}
if (string.IsNullOrEmpty(userPassword.Text) || string.IsNullOrEmpty(userPasswordConfirm.Text))
{
Toast.MakeText(this, "Invalid Password Invalid", ToastLength.Short).Show();
}
else
{
if (userPassword.Text != userPasswordConfirm.Text)
{
Toast.MakeText(this, "Passwords not the same", ToastLength.Short).Show();
}
else
{
PasswordHasher hasher = new PasswordHasher();
byte[] saltBytes = hasher.GenerateRandomSalt(PasswordHasher.saltByteSize);
string saltString = Convert.ToBase64String(saltBytes);
string passwordHash = hasher.PBKDF2_SHA256_GetHash(userPassword.Text, saltString,
PasswordHasher.iterations, PasswordHasher.hashByteSize);
Console.WriteLine("SALT: " + saltString.Length);
Console.WriteLine("HASH: " + passwordHash.Length);
string hashedPwd = saltString + passwordHash;
player.PlayerPassword = hashedPwd;
passBool = true;
}
}
//If everything is correct insert into database
if (nameBool && emailBool && passBool)
{
player.PlayerPoints = 0; //Standard is 0
playerQuery.Insert(player);
Toast.MakeText(this, "Thank you for registering, please login", ToastLength.Long).Show();
StartActivity(typeof(LoginActivity));
}
sendToRegisterButton.Enabled = true;
}
}
}
【问题讨论】:
-
也许可以使用另一种方法 - 单击时禁用按钮,然后在完成处理后重新启用按钮?
-
也许可行,我会实施它,看看是否可行 :) 编辑:它不起作用。它仍然连续发射大约 3 次。
-
我同意@sleeyuen 我会通过将sendToRegisterButton 的Enabled 属性设置为false 来禁用您的按钮,然后执行checkFields 然后将enabled 设置回true。
-
不幸的是,这种方法也不起作用。我实现它的方式是在 OP
-
我认为我们需要更多信息,因为上面代码中显示的内容必须更多。您是否正在做类似重用视图或类似的事情?你在这里看到这个链接了吗? forums.xamarin.com/discussion/9244/…
标签: c# events xamarin delegates xamarin.android