zoukankan      html  css  js  c++  java
  • Windows Phone 7 使用Perst数据库的Demo——流水账

       在Windows Phone 7程序项目中使用Perst,需要引用PerstWP7.dll,dll文件可以到Perst的官方网站上下载。这个perst数据库的demo简单地实现了记账保存功能和流水账查询的功能,旨在用最简单最简洁的代码在Windows Phone 7上使用Perst数据库。

    程序截图如下:

    先从App.xaml文件说起

    因为数据库对象是相对于整个程序来说的,所以一般会在App.xaml.cs中进行创建 初始化和关闭

    App.xaml.cs

    public Database Database { get; internal set; } //定义一个数据库对象


    internal void ClosePerstDatabase()
    {
    if (Database != null && Database.Storage != null)
    Database.Storage.Close();
    //关闭数据库存储
    }

    public App()
    {
    InitializePerstStorage();
    ……
    }

    internal void InitializePerstStorage()
    {
    Storage storage
    = StorageFactory.Instance.CreateStorage(); //创建Perst存储Storage实例
    storage.SetProperty("perst.file.extension.quantum", 512 * 1024); //初始化存储大小为512KB
    storage.SetProperty("perst.extension.quantum", 256 * 1024); //每次递增的存储大小为 256KB

    storage.Open(
    "PerstDemoDB.dbs", 0); // 打开Storage

    //使用上面初始化的Storage实例创建数据库
    Database = new Database(storage, false, true, new FullTextSearchHelper(storage));
    //Database =new Perst.Database(
    Database.EnableAutoIndices = false; //关闭自动索引 即使用人工索引
    }

    private void Application_Closing(object sender, ClosingEventArgs e)
    {
    ClosePerstDatabase();
    //关闭数据库
    }

    再来看看 ViewModel的文件

    Account.cs

    using System.ComponentModel;
    using System.Linq;
    using System.Globalization;
    using Perst.FullText;
    using System.Collections.Generic;
    using System;
    using Perst;

    namespace PerstDemo.ViewModel
    {
    public class Account : Persistent, INotifyPropertyChanged
    {
    [FullTextIndexable]
    //FullTextIndexable定义为索引
    public string inOrOut; //收入或者支出
    [FullTextIndexable]
    public string time; //时间
    [FullTextIndexable]
    public string money; //多少钱
    [FullTextIndexable]
    public string description; //描述

    public override void OnLoad()
    {
    base.OnLoad();
    }

    public string InOrOut
    {
    get { return inOrOut; }
    set
    {
    inOrOut
    = value;
    InvokePropertyChanged(
    new PropertyChangedEventArgs("InOrOut"));
    }
    }

    public string Time
    {
    get { return time; }
    set
    {
    time
    = value;
    InvokePropertyChanged(
    new PropertyChangedEventArgs("Time"));
    }
    }

    public string Money
    {
    get { return money; }
    set
    {
    money
    = value;
    InvokePropertyChanged(
    new PropertyChangedEventArgs("Money"));
    }
    }

    public string Description
    {
    get { return description; }
    set
    {
    this.description = value;
    InvokePropertyChanged(
    new PropertyChangedEventArgs("Description"));
    }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    //删除对象的数据
    public override void Deallocate()
    {
    base.Deallocate();
    }

    private void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
    var handler
    = PropertyChanged;
    if (handler != null) handler(this, e);
    }
    }
    }

    AccountsViewModel.cs

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    using Perst;

    namespace PerstDemo.ViewModel
    {
    public class AccountsViewModel : INotifyPropertyChanged
    {
    public AccountsViewModel()
    {
    Accounts
    = new ObservableCollection<Account>();

    //从数据库中获取所有的Account记录
    if (Database != null)
    {
    //数据库查询 查询出Account类(相当于表)的所有对象 通过时间进行排序
    Accounts = Database.Select<Account>("order by Time").ToObservableCollection(); // Load them but sorted
    }
    }

    public ObservableCollection<Account> Accounts { get; private set; }

    private static Database Database
    {
    get { return ((App)Application.Current).Database; }
    }

    private static Storage Storage
    {
    get { return Database.Storage; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
    if (null != PropertyChanged)
    {
    PropertyChanged(
    this, new PropertyChangedEventArgs(propertyName));
    }
    }

    }
    }

    流水账页面  绑定数据库Account表的所有记录

    View Code
    <phone:PhoneApplicationPage
    x:Class="PerstDemo.MainPage"
    xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone
    ="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell
    ="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d
    ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
    ="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable
    ="d" d:DesignWidth="480" d:DesignHeight="696"
    FontFamily
    ="{StaticResource PhoneFontFamilyNormal}"
    FontSize
    ="{StaticResource PhoneFontSizeNormal}"
    Foreground
    ="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations
    ="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible
    ="True"
    xmlns:vm
    ="clr-namespace:PerstDemo.ViewModel"
    >



    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock x:Name="PageTitle" Text="流水账" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox x:Name="AccountsListBox" Grid.Row="1" >
    <ListBox.ItemTemplate>
    <DataTemplate>
    <StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
    <StackPanel>
    <StackPanel Orientation="Horizontal">
    <TextBlock x:Name="inout" Text="{Binding InOrOut}" Margin="0,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
    <TextBlock x:Name="money" Text="{Binding Money}" Margin="10,0,0,5" VerticalAlignment="Bottom" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock x:Name="time" Text="{Binding Time}" Margin="70,0,0,0" VerticalAlignment="Bottom" Style="{StaticResource PhoneTextNormalStyle}"/>
    </StackPanel>
    <TextBlock x:Name="DetailsText" Text="{Binding Description}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextAccentStyle}"/>
    </StackPanel>
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>
    </Grid>
    </Grid>
    <phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
    <shell:ApplicationBar.MenuItems>
    <shell:ApplicationBarMenuItem Text="记一笔账" Click="New_Click"/>
    </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>


    </phone:PhoneApplicationPage>
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using PerstDemo.ViewModel;
    using Perst;
    using System.Collections.ObjectModel;

    namespace PerstDemo
    {
    public partial class MainPage : PhoneApplicationPage
    {
    // Constructor
    public MainPage()
    {
    InitializeComponent();
    this.AccountsListBox.ItemsSource = new AccountsViewModel().Accounts;
    }

    private void New_Click(object sender, EventArgs e)
    {
    NavigationService.Navigate(
    new Uri("/AddAccount.xaml", UriKind.Relative));
    }
    }
    }

    记账页面  添加一条Account表的记录

    AddAccount.xaml

    View Code
    <phone:PhoneApplicationPage
    x:Class="PerstDemo.AddAccount"
    xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone
    ="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell
    ="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d
    ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
    ="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily
    ="{StaticResource PhoneFontFamilyNormal}"
    FontSize
    ="{StaticResource PhoneFontSizeNormal}"
    Foreground
    ="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations
    ="Portrait" Orientation="Portrait"
    mc:Ignorable
    ="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible
    ="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock x:Name="PageTitle" Text="记账" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <RadioButton Content="收入" Height="92" HorizontalAlignment="Left" Margin="62,29,0,0" Name="income" FontSize="30" VerticalAlignment="Top" />
    <RadioButton Content="支出" Height="92" HorizontalAlignment="Left" Margin="256,29,0,0" Name="outgo" FontSize="30" VerticalAlignment="Top" />
    <TextBlock Height="43" HorizontalAlignment="Left" Margin="23,169,0,0" Name="textBlock1" Text="金额" FontSize="30" VerticalAlignment="Top" Width="100" />
    <TextBox Height="72" HorizontalAlignment="Left" Margin="115,152,0,0" Name="money" Text="" VerticalAlignment="Top" Width="312" />
    <TextBlock FontSize="30" Height="43" HorizontalAlignment="Left" Margin="19,250,0,0" Name="textBlock2" Text="备注" VerticalAlignment="Top" Width="100" />
    <TextBox Height="72" HorizontalAlignment="Left" Margin="115,234,0,0" Name="desc" Text="" VerticalAlignment="Top" Width="312" />
    <TextBlock FontSize="30" Height="43" HorizontalAlignment="Left" Margin="19,344,0,0" Name="textBlock3" Text="时间" VerticalAlignment="Top" Width="100" />
    <TextBox Height="72" HorizontalAlignment="Left" Margin="115,330,0,0" Name="time" Text="" VerticalAlignment="Top" Width="312" />
    <Button FontSize="40" Content="保存" Height="111" HorizontalAlignment="Left" Margin="43,466,0,0" Name="button1" VerticalAlignment="Top" Width="384" Click="button1_Click" />
    </Grid>
    </Grid>

    </phone:PhoneApplicationPage>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using Perst;
    using PerstDemo.ViewModel;

    namespace PerstDemo
    {
    public partial class AddAccount : PhoneApplicationPage
    {
    public AddAccount()
    {
    InitializeComponent();
    }
    //保存记录
    private void button1_Click(object sender, RoutedEventArgs e)
    {
    Database Database
    = ((App)App.Current).Database;//获取在App中定义的数据库对象
    string inorout = "支出";
    if (this.income.IsChecked != null && this.income.IsChecked == true)
    {
    inorout
    = "收入";
    }
    //初始化一个表对象
    Account tem1 = new Account { InOrOut = inorout, Money = this.money.Text, Description = this.desc.Text,Time=this.time.Text };
    Database.AddRecord(tem1);
    //添加数据库记录
    Database.Storage.Commit();//关闭数据库存储
    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));//跳转到首页的流水账显示
    }
    }
    }

    因为查询的记录的结果是 IEnumerable<T>类型  数据绑定使用了ObservableCollection<T>类型  所用需要转换一下

    Utilities.cs

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Collections.ObjectModel;
    using System.Collections.Generic;

    namespace PerstDemo
    {
    public static class Utilities
    {
    //将IEnumerable<T>转化为ObservableCollection<T> 用于数据绑定
    public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> source)
    {
    var collection
    = new ObservableCollection<T>();
    foreach (var acount in source)
    collection.Add(acount);
    return collection;
    }
    }
    }
  • 相关阅读:
    React源码 Suspense 和 ReactLazy
    React源码 ReactContext
    BZOJ 3456: 城市规划 与 多项式求逆算法介绍(多项式求逆, dp)
    LOJ #6436. 「PKUSC2018」神仙的游戏(字符串+NTT)
    LOJ #6433. 「PKUSC2018」最大前缀和(状压dp)
    LOJ #6432. 「PKUSC2018」真实排名(组合数)
    LOJ #2542. 「PKUWC 2018」随机游走(最值反演 + 树上期望dp + FMT)
    LOJ #2541. 「PKUWC 2018」猎人杀(容斥 , 期望dp , NTT优化)
    LOJ #2540. 「PKUWC 2018」随机算法(概率dp)
    LOJ #2538. 「PKUWC 2018」Slay the Spire (期望dp)
  • 原文地址:https://www.cnblogs.com/linzheng/p/2001532.html
Copyright © 2011-2022 走看看