我想为用户登录时创建一个用户个人资料,以便查看其用户名和其他内容。我相信我做得正确,但是有问题。 当我尝试通过Login方法将数据添加到_name和_class时,不要添加数据。
这是登录页面:
public partial class LoginPage : Rg.Plugins.Popup.Pages.PopupPage
{
SQLiteConnection _sqlconnection = DependencyService.Get<ISQLiteConnection>().GetConnection();
Users _user = new Users();
public string _name = "name";
public string _class = "class";
SQLiteManager manager;
public LoginPage ()
{
InitializeComponent ();
manager = new SQLiteManager();
}
private async void btnLogin_Clicked(object sender, EventArgs e)
{
var username = txtName.Text;
var password = txtPassword.Text;
var loginState = Login(username, password);
if (loginState)
{
if (txtName.Text != null || txtPassword.Text != null)
{
await Navigation.PushModalAsync(new StartPage());
await PopupNavigation.Instance.PopAsync();
}
else
{
await DisplayAlert("ERROR", "You can't leave it blank.", "OK");
}
}
else if (!loginState)
{
await DisplayAlert("ERROR", "There is no user", "OK");
await PopupNavigation.Instance.PopAsync();
}
}
public bool Login(string Username, string Password)
{
string mySelectQuery = "SELECT Count(*) FROM Users WHERE Name='" + Username + "' AND Password='" + Password + "'";
string getId = "SELECT Id FROM Users WHERE Name='" + Username + "' AND Password='" + Password + "'";
var user = _sqlconnection.CreateCommand(mySelectQuery);
var Id = _sqlconnection.CreateCommand(getId);
var count = user.ExecuteScalar<int>();
var id = Id.ExecuteScalar<int>();
if (count > 0)
{
return true;
}
var profileUser = manager.Get6(id);
_name = profileUser.Name;
_class = profileUser.Class;
return false;
}
private async void btnCancel_Clicked(object sender, EventArgs e)
{
await PopupNavigation.Instance.PopAsync();
}
}
以及此处的个人资料页面:
public partial class DetailPage3 : ContentPage
{
LoginPage _login = new LoginPage();
public DetailPage3 ()
{
InitializeComponent ();
lblName.Text = _login._name;
lblClass.Text = _login._class;
}
}