【问题标题】:Xamarin button click not workingXamarin 按钮单击不起作用
【发布时间】:2016-08-20 00:46:34
【问题描述】:

我正在尝试像在 Windows 论坛中那样单击按钮,但是我做错了,因为没有任何内容写入控制台,并且按钮的文本没有改变。为了测试目的,我更改了按钮文本。 我也知道我连接到 SQL 数据库的方法是不安全的,但是这个应用程序是严格供个人使用的,并且现在是学习目的。无论如何,这是我的代码:

Main.axml 代码:

<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 代码:

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

【问题讨论】:

  • 您在哪里向您的Button 添加事件处理程序?我只是回忆以前的 C# + .Net 经验。
  • 如果我没记错的话,应该是这个吧? mBtnSignIn = FindViewById
  • 这只是找到按钮对象。您的按钮对象需要告诉它应该调用哪个代码来处理事件,例如单击。在教程和代码示例中查看它是如何完成的。

标签: c# android visual-studio xamarin


【解决方案1】:

您需要将按钮mBtnSignIn 与处理按钮点击的事件处理程序mBtnSignIn_Click 连接起来。这不是简单的,通过命名,你必须手动完成。

最简单的方法是订阅按钮的Click 事件并将处理程序添加到其中:

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

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

    // ...
}

【讨论】:

    【解决方案2】:

    您似乎正在使用 Xamarin.Android,一切都很好,只是您没有向按钮添加事件处理程序来处理单击事件。可以通过 3 种简单的方式完成。

    @robinmanuelthiel 在这里提到了一个,另外两个是:

    使用 Lambda 表达式:

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

    使用委托语法:

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

    在您的情况下,最好将事件处理程序添加到您的按钮。 在这里您可以找到更多帮助和代码示例https://developer.xamarin.com/

    谢谢

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,没有在按钮上调用 Clicked 回调。

      这么愚蠢 - 我花了 小时 才弄明白 - 问题是我在其中一个父控件上设置了 InputTransparent="True" -_-

      【讨论】:

        猜你喜欢
        • 2023-04-05
        • 2018-05-02
        • 2016-06-07
        • 2014-03-24
        • 2012-01-29
        • 2017-06-08
        相关资源
        最近更新 更多