maui sqlite开发一个商城加购物车的演示(1)

界面演示



csharp 复制代码
using ShoppingUI;

namespace ShoppingUI;

public partial class App : Application
{

    public App()
	{
		InitializeComponent();
		MainPage = new LoginPage();
	}
    static LoginDatabase database;

    // Create the database connection as a singleton.
    public static LoginDatabase Database
    {
        get
        {
            if (database == null)
            {
                database = new LoginDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "shop.db"));
            }
            return database;
        }
    }
}

上在是sqlite的链接。

csharp 复制代码
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ShoppingUI.LoginPage"
             Title="登陆">
    <ContentPage.Content>
        <Grid>
            <Image Source="landscape.png" VerticalOptions="Center" Aspect="Center"/>
            <Grid HorizontalOptions="Center" VerticalOptions="Center" Padding="10">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <Label Grid.Row="0" Grid.Column="0" Text="手机号" FontSize="17" FontAttributes="Bold" TextColor="#2C363C"/>
                <Frame Grid.Row="1" Grid.Column="0" HeightRequest="50" >
                    <!-- 使用 Keyboard 属性设置为 Telephone,MaxLength 设置为 11 -->
                    <Entry  Text="{Binding UserName}" BackgroundColor="White" HeightRequest="48"  WidthRequest="200" Keyboard="Telephone" MaxLength="11"/>
                </Frame>
                <Label Grid.Row="2" Text="密码" Padding="0,20,0,0" FontAttributes="Bold" FontSize="17" TextColor="#2C363C"/>
                <Frame Grid.Row="3" HeightRequest="50">
                    <!-- 使用 Keyboard 属性设置为 Numeric,MaxLength 设置为 6 -->
                    <Entry Text="{Binding Password}" IsPassword="True" BackgroundColor="White" HeightRequest="48"  WidthRequest="200" Keyboard="Numeric" MaxLength="6"/>
                </Frame>
                <Grid Grid.Row="4" Padding="0,40,0,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <Button Grid.Row="4" Grid.Column="0" Text="登陆" Command="{Binding LoginCommand}" TextColor="White" BackgroundColor="#2C363C" HeightRequest="40" WidthRequest="100"/>
                    <Button Grid.Row="4" Grid.Column="1" Text="注册" Command="{Binding RegisterCommand}" TextColor="White" BackgroundColor="#2C363C" HeightRequest="40" WidthRequest="100"  Margin="10,0,0,0" />
                </Grid>
            </Grid>
        </Grid>
    </ContentPage.Content>
</ContentPage>
csharp 复制代码
namespace ShoppingUI;
下面this.BindingContext = new LoginViewModel(this.Navigation);将LoginViewModel绑定到xaml.
public partial class LoginPage : ContentPage
{
	public LoginPage()
	{
		InitializeComponent();
        this.BindingContext = new LoginViewModel(this.Navigation);
       
    }
}
csharp 复制代码
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingUI
{
    public class LoginDatabase
    {
        readonly SQLiteAsyncConnection database;

        public LoginDatabase(string dbPath)
        {
            database = new SQLiteAsyncConnection(dbPath);
            database.CreateTableAsync<LoginModel>().Wait();
        }

        public Task<LoginModel> GetLoginDataAsync(string userName)
        {
            return database.Table<LoginModel>()
                            .Where(i => i.UserName == userName)
                            .FirstOrDefaultAsync();
        }

        public Task<int> SaveLoginDataAsync(LoginModel loginData)
        {
            return database.InsertAsync(loginData);
        }
    }
}
相关推荐
远歌已逝2 小时前
维护在线重做日志(二)
数据库·oracle
qq_433099403 小时前
Ubuntu20.04从零安装IsaacSim/IsaacLab
数据库
Dlwyz3 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
Theodore_10224 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
工业甲酰苯胺5 小时前
Redis性能优化的18招
数据库·redis·性能优化
冰帝海岸5 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象5 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了6 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·6 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic6 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端