저는 Windows 포럼에서와 같이 버튼을 클릭하려고합니다. 그러나 콘솔에 아무 것도 쓰여지지 않고 버튼의 텍스트가 변경되지 않기 때문에 뭔가 잘못하고 있습니다. 테스트 목적으로 버튼 텍스트가 변경되었습니다. 또한 SQL 데이터베이스 연결 방법이 안전하지 못하다는 것을 알고 있습니다. 그러나이 응용 프로그램은 개인 용도로만 사용할 수 있으며 현재 학습 목적으로 사용하고 있습니다.Xamarin 버튼 클릭이 작동하지 않습니다.
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 코드 : 당신은을 처리하는 이벤트 핸들러 mBtnSignIn_Click
, 당신의 버튼 mBtnSignIn
을 연결해야
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 경험에 대한 기억을 떠 올릴뿐입니다. – ray
내가 틀리지 않으면, 그게 이거 야? mBtnSignIn = FindViewById
그냥 단추 개체를 찾습니다. 버튼 객체는 클릭과 같은 이벤트를 처리하기 위해 호출해야하는 코드를 알려야합니다. 자습서 및 코드 샘플에서 어떻게 수행되었는지 확인하십시오. – ray