I want to login using Xamaring forms with MVVM and SQLite Database. I created a LoginModel.cs, LoginPage.xaml and LoginPage.xaml.cs.
My Questions are:
1. How can I create a local database?
2. How can I create, updated, edit and delete data to local database?<br/>
3. How can it automatically go to homepage if the username and password type is equal to the data inside the local database?
(Note: my codes are below)
LoginPage.xaml(This is where I bind my entry and button)
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TBSMobileApplication.View.LoginPage"
BackgroundColor="#ecf0f1">
<ContentPage.Content>
<StackLayout
VerticalOptions="StartAndExpand">
<StackLayout.Padding>
<OnPlatform
x:TypeArguments="Thickness"
iOS="20"
Android="20,100,20,0">
</OnPlatform>
</StackLayout.Padding>
<Label
Text="Username"
TextColor="#34495e"
Font="Arial,10"/>
<Entry
Placeholder="Username"
PlaceholderColor="#95a5a6"
FontSize="12"
FontFamily="Arial"
x:Name="entUsername"
Text="{Binding Username}"/>
<Label
Text="Password"
TextColor="#34495e"
Font="Arial,10"/>
<Entry
Placeholder="Password"
PlaceholderColor="#95a5a6"
FontSize="12"
FontFamily="Arial"
IsPassword="True"
x:Name="entPassword"
Text="{Binding Password}"/>
<Button
Text="Login"
FontSize="12"
HorizontalOptions="Start"
BackgroundColor="#3498db"
Command="{Binding SubmitCommand}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
LoginPage.xaml.cs (This is where the functions are executed)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TBSMobileApplication.Model;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace TBSMobileApplication.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPage
{
public UserModel userModel;
public LoginPage ()
{
InitializeComponent ();
userModel = new UserModel();
MessagingCenter.Subscribe<UserModel, string>(this,"Login Alert",(sender, username) =>
{
DisplayAlert("", username, "Ok");
});
this.BindingContext = userModel;
entUsername.Completed += (object sender, EventArgs e) =>
{
entPassword.Focus();
};
entPassword.Completed += (object sender, EventArgs e) =>
{
userModel.SubmitCommand.Execute(null);
};
}
}
}
UserModel.cs(This is where I get the values and functions)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
namespace TBSMobileApplication.Model
{
public class UserModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string username;
public string Username
{
get { return username; }
set
{
username = value;
PropertyChanged(this, new PropertyChangedEventArgs("Username"));
}
}
public string password;
public string Password
{
get { return password; }
set
{
password = value;
PropertyChanged(this, new PropertyChangedEventArgs("Password"));
}
}
public ICommand SubmitCommand { get; set; }
public UserModel()
{
SubmitCommand = new Command(OnSubmit);
}
public void OnSubmit()
{
if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
{
MessagingCenter.Send(this, "Login Alert", "Please fill-up the form");
}
else
{
MessagingCenter.Send(this, "Login Alert", Username + " & " + Password);
}
}
}
}
What I have tried:
I can get the data I typed using a "**DISPLAYALERT**" through "**MessagingCenter**" from the text box. I don't know how to proceed in creating a local database to be able to login and go to the main page.