使用带有MVVM和SQLITE数据库的XAMARIN FORMS登录 [英] Login using XAMARIN FORMS with MVVM and SQLITE database

查看:94
本文介绍了使用带有MVVM和SQLITE数据库的XAMARIN FORMS登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有MVVM和SQLite数据库的Xamaring表单进行登录。我创建了一个LoginModel.cs,LoginPage.xaml和LoginPage.xaml.cs。 

我的问题是:
1.如何创建本地数据库?
2.如何创建,更新,编辑和删除数据到本地数据库?< br />
3.如果用户名和密码类型等于本地数据库中的数据,它如何自动转到主页?


(注意:我的代码在下面)
LoginPage.xaml(这是我绑定我的条目和按钮的地方)

<?xml version =1.0encoding =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
占位符=密码
PlaceholderColor =#95a5a6
FontSize =12
FontFamily =Arial
IsPassword = True
x:Name =entPassword
Text ={Binding Password}/>
< Button
Text =Login
FontSize =12
Horizo​​ntalOptions =Start
BackgroundColor =#3498db
Command = {Binding SubmitCommand}/>
< / StackLayout>
< /ContentPage.Content>
< / ContentPage>

LoginPage.xaml.cs(这是函数执行的地方)

using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用TBSMobileApplication.Model;
使用Xamarin.Forms;
使用Xamarin.Forms.Xaml;

命名空间TBSMobileApplication.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage:ContentPage
{
public UserModel的usermodel;

public LoginPage()
{
InitializeComponent();
userModel = new UserModel();
MessagingCenter.Subscribe< UserModel,string>(此,登录提醒,(发件人,用户名)=>
{
DisplayAlert(,用户名,确定);
});
this.BindingContext = userModel;

entUsername.Completed + =(object sender,EventArgs e)=>
{
entPassword.Focus();
};

entPassword.Completed + =(object sender,EventArgs e)=>
{
userModel.SubmitCommand.Execute(null);
};
}
}
}

UserModel.cs(这是我获取值和函数的地方)

using System;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Text;
使用System.Windows.Input;
使用Xamarin.Forms;

namespace TBSMobileApplication.Model
{
public class UserModel:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
公共字符串用户名;
public string用户名
{
get {return username; }
设置
{
username = value;
PropertyChanged(this,new PropertyChangedEventArgs(Username));
}
}

公共字符串密码;
公共字符串密码
{
get {return password; }
设置
{
密码=值;
PropertyChanged(这是新的PropertyChangedEventArgs(密码));
}
}

public ICommand SubmitCommand {get;组; }

public UserModel()
{
SubmitCommand = new Command(OnSubmit);
}

public void OnSubmit()
{
if(string.IsNullOrEmpty(Username)|| string.IsNullOrEmpty(Password))
{
MessagingCenter.Send(这是登录提醒,请填写表格);
}
其他
{
MessagingCenter.Send(此处,登录提醒,用户名+&+密码);
}

}
}
}





我尝试了什么:



我可以使用** DISPLAYALERT **通过*获取我输入的数据* MessagingCenter **来自文本框。我不知道如何继续创建本地数据库以便能够登录并转到主页面。

解决方案

正在进行快速搜索每个问题应该会给你很多例子。以下是其中一些:



Xamarin登录示例:https://www.c-sharpcorner.com/article/xamarin-forms-create-a-login -page-mvvm /



官方文档还提供了如何使用本地数据库的快速指南:Xamarin.Forms本地数据库 - Xamarin | Microsoft Docs [ ^ ]

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.

解决方案

Doing a quick search for each of your question should give you a lot of examples. Here are a few of them:

Xamarin Login Example: https://www.c-sharpcorner.com/article/xamarin-forms-create-a-login-page-mvvm/

The official documenation also has a quick guide on how to use Local database: Xamarin.Forms Local Databases - Xamarin | Microsoft Docs[^]


这篇关于使用带有MVVM和SQLITE数据库的XAMARIN FORMS登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆