创建SQLite表Xamarin表单 - c#

我想创建一个多个SQLite表,但是我不知道如何进行。

我的项目流程:
-当我单击登录按钮时,如果填写了用户名和密码,它应该创建SQLite数据库和表。

需要创建表:
用户表(字段:UserID(字符串),UsrPassword(字符串),ContactId(int),Status(字符串))
零售商(字段:零售商名称(字符串),零售商处理程序(整数))

我已经完成的工作:
1.我已经添加了SQLite Nuget包
2.我添加了SQLiteAsyncConnection GetConnection();到我的界面
3.我为每个项目(Android和UWP)添加了数据库创建器
4.我已经将我的LoginPage表单绑定到我的ViewModel

我的代码如下:

ISQLiteDB.cs

using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace TBSMobileApplication.Data
{
    public interface ISQLiteDB
    {
        SQLiteAsyncConnection GetConnection();
    }
}

LoginPage.xaml

<?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 LoginCommand}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

LoginPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TBSMobileApplication.ViewModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TBSMobileApplication.View
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginPage : ContentPage
    {
        public LoginPage ()
        {
            InitializeComponent ();
            BindingContext = new LoginPageViewModel();
            MessagingCenter.Subscribe<LoginPageViewModel,string>(this, "Login Alert",(sender,Username) =>
            {
                DisplayAlert("Login Alert", "Please fill-up the form", "Ok");
            });
        }
    }
}

LoginPageViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Input;
using TBSMobileApplication.View;
using Xamarin.Forms;

namespace TBSMobileApplication.ViewModel
{
    public class LoginPageViewModel : INotifyPropertyChanged
    {
        void OnProperyChanged(string PropertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }

        public string username;
        public string password;

        public string Username
        {
            get { return username; }
            set
            {
                username = value;
                OnProperyChanged(nameof(Username));
            }
        }

        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                OnProperyChanged(nameof(Password));
            }
        }

        public ICommand LoginCommand { get; set; }

        public LoginPageViewModel()
        {
            LoginCommand = new Command(OnLogin);
        }

        public void OnLogin()
        {
            if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
            {
                MessagingCenter.Send(this, "Login Alert", Username);
            }
            else
            {

            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

AndroidSQLiteDB.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SQLite;
using TBSMobileApplication.Data;
using TBSMobileApplication.Droid.Data;
using Xamarin.Forms;

[assembly: Dependency(typeof(AndroidSQLiteDb))]

namespace TBSMobileApplication.Droid.Data
{
    public class AndroidSQLiteDb : ISQLiteDB
    {
        public SQLiteAsyncConnection GetConnection()
        {
            var dbFileName = "backend.db3";
            var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var path = Path.Combine(documentsPath, dbFileName);

            return new SQLiteAsyncConnection(path);
        }
    }
}

WindowsSQLiteDB.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite;
using TBSMobileApplication.Data;
using TBSMobileApplication.UWP.Data;
using Windows.Storage;
using Xamarin.Forms;

[assembly: Dependency(typeof(WindowsSQLiteDb))]

namespace TBSMobileApplication.UWP.Data
{
    public class WindowsSQLiteDb : ISQLiteDB
    {
        public SQLiteAsyncConnection GetConnection()
        {
            var dbFileName = "backend.db3";
            var documentsPath = ApplicationData.Current.LocalFolder.Path;
            var path = Path.Combine(documentsPath, dbFileName);
            return new SQLiteAsyncConnection(path);
        }
    }
}

参考方案

您不想在用户单击按钮时创建表-应该在您的应用在给定设备上首次启动时真正创建数据库和表。但是程序是一样的

// get the connection
var db = DependencyService.Get< ISQLiteDB>();
var conn = db.GetConnection();

// create the tables
if (conn != null) {
  await conn.CreateTableAsync<Users>();
  await conn.CreateTableAsync<Retailer>();
}

并且每个数据库模型都需要一个类

public class Retailer {
  public string Retailer_Name { get; set; }
  public int Retailer_Handler { get; set; }
}

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

将python scikit学习模型导出到pmml - python

我想将python scikit-learn模型导出到PMML。哪个python软件包最合适?我阅读了有关Augustus的内容,但是我无法使用scikit-learn模型找到任何示例。 python大神给出的解决方案 SkLearn2PMML是 JPMML-SkLearn命令行应用程序周围的薄包装。有关受支持的Scikit-Learn Estimator和…

故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php

我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…

将Web用户控件添加到页面时,asp按钮onclick不会触发 - javascript

我正在使用使用Visual Studio模板创建的Web表单应用程序。模板具有一个内容占位符,该占位符被访问的任何页面的内容替换。有问题的页面有几个服务器控件,例如文本框,标签和按钮。当我单击我的更新按钮时,它可以正常工作,这些值会回传并更新数据库。我想在所有子页面上创建通用的登录提示。我的导航栏(位于我的母版页中)具有引导程序设置。我在导航栏中添加了一个下…

HTML Action ID获取方法 - javascript

这可能已经是另一个问题的副本,但我找不到它。我采取了以下行动:<form id="bug-form" action="/AST_14/Bugg.ly2/index.php/bug/update/" method="post"> 现在,我的系统的工作方式是每个需要更新的“错误”都有自己的错…