简体   繁体   中英

Xamarin button click not working

I'm trying to make a Button Click like in Windows Forum however, I'm doing something wrong, because nothing is written to console, and the text of the button doesn't change. I have the button text changing for testing purposes. Also I am aware that my method for connecting to the SQL Database is insecure, however this application is strictly for personal use, and as-of right now learning purposes. Anyways, here's my code:

Main.axml Code:

<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:background="@drawable/buttonstyle"
android:id="@+id/buttonLogIn"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:drawableLeft="@drawable/lockicon"
android:textStyle="bold"
android:textColor="#FFFFFF" />

MainActivity.cs Code:

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using CryptSharp;
using MySql.Data.MySqlClient;
using System.Data;

namespace App1
{
    [Activity(Label = "TexByte", MainLauncher = true, Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen", Icon = "@drawable/Logo_Mob2")]
    public class MainActivity : Activity
    {
        private EditText mtxtUsername, mtxtPassword;
        private Button mBtnSignIn;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);
            mBtnSignIn = FindViewById<Button>(Resource.Id.buttonLogIn);
            mtxtUsername = FindViewById<EditText>(Resource.Id.txtUsername);
            mtxtPassword = FindViewById<EditText>(Resource.Id.txtPassword);
        }
        private void mBtnSignIn_Click(object sender, EventArgs args)
        {
            MySqlConnection con = new MySqlConnection("Server=127.0.0.1;User Id=root;Password=password;Database=login;");
            try
            {
                string username = mtxtUsername.Text;
                string password = mtxtPassword.Text;

                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                    MySqlCommand cmd = new MySqlCommand("SELECT * FROM members WHERE username = '" + username + "' order by password ");
                    cmd.CommandType = CommandType.Text;
                    MySqlDataReader rdr = cmd.ExecuteReader();

                    while (rdr.Read())
                    {

                        string hash = string.Format("{0}", rdr["password"]);
                        Console.WriteLine(hash);

                        if (Crypter.CheckPassword(password, hash))
                        {
                            Console.WriteLine("User name and Password Success ");
                            mBtnSignIn.Text = "Test";
                        }
                        else
                        {
                            Console.WriteLine("Unable to process request. Verify username and password are correct.");
                            mBtnSignIn.Text = "Fail";
                        }
                    }
                }
            }
            catch(MySqlException ex)
            {
                Console.WriteLine("ERROR: Something went wrong. :( ", ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }
}

You need to connect your button mBtnSignIn with your event handler mBtnSignIn_Click , that processes the button click. That does not simply happen, through naming, you have to do it by hand.

The simplest way to do that is subscibing your button's Click event and add your handler to it:

protected override void OnCreate(Bundle bundle)
{
    // ...

    mBtnSignIn = FindViewById<Button>(Resource.Id.buttonLogIn);
    mBtnSignIn.Click += mBtnSignIn_Click;

    // ...
}

It seems like you are working on Xamarin.Android, everything is fine except that you haven't added an event handler to your button for handling the click event. It can be done in 3 simple ways.

one is mentioned by @robinmanuelthiel here the other two are :

Using the Lambda expression:

mBtnSignIn.Click += (object o, EvenrArgs e) => {
    //Write your Code..
};

Using the Delegate syntax:

mBtnSignIn.Click += delegate {
    //Write your Code..
};

In your case it is better if you just add the eventhandler to your button. Here you can find more help and code samples https://developer.xamarin.com/ .

Thanks

I had a similar problem, with the Clicked callback not being called on a button.

As stupid as this is - and it took me hours to figure out - the problem was that I had InputTransparent="True" set on one of the parent controls -_-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM