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

 

 
  1. public Database Database { get; internal set; }  //定义一个数据库对象  
  2.  
  3.  
  4.         internal void ClosePerstDatabase()  
  5.         {  
  6.             if (Database != null && Database.Storage != null)  
  7.                 Database.Storage.Close();//关闭数据库存储  
  8.         }  
  9.  
  10.         public App()  
  11.         {  
  12.             InitializePerstStorage();  
  13.             ……  
  14.          }  
  15.  
  16.         internal void InitializePerstStorage()  
  17.         {  
  18.             Storage storage = StorageFactory.Instance.CreateStorage(); //创建Perst存储Storage实例  
  19.             storage.SetProperty("perst.file.extension.quantum", 512 * 1024); //初始化存储大小为512KB  
  20.             storage.SetProperty("perst.extension.quantum", 256 * 1024); //每次递增的存储大小为 256KB   
  21.  
  22.             storage.Open("PerstDemoDB.dbs", 0); // 打开Storage  
  23.  
  24.             //使用上面初始化的Storage实例创建数据库  
  25.             Database = new Database(storage, false, true, new FullTextSearchHelper(storage));  
  26.             //Database =new Perst.Database(  
  27.             Database.EnableAutoIndices = false; //关闭自动索引 即使用人工索引  
  28.         }  
  29.  
  30.         private void Application_Closing(object sender, ClosingEventArgs e)  
  31.         {  
  32.             ClosePerstDatabase();//关闭数据库  
  33.         } 

再来看看 ViewModel的文件

Account.cs

 

 
  1. using System.ComponentModel;  
  2. using System.Linq;  
  3. using System.Globalization;  
  4. using Perst.FullText;  
  5. using System.Collections.Generic;  
  6. using System;  
  7. using Perst;  
  8.  
  9. namespace PerstDemo.ViewModel  
  10. {  
  11.     public class Account : Persistent, INotifyPropertyChanged  
  12.     {  
  13.         [FullTextIndexable]//FullTextIndexable定义为索引  
  14.         public string inOrOut;  //收入或者支出  
  15.         [FullTextIndexable]  
  16.         public string time;  //时间  
  17.         [FullTextIndexable]  
  18.         public string money;  //多少钱  
  19.         [FullTextIndexable]  
  20.         public string description;  //描述  
  21.  
  22.         public override void OnLoad()  
  23.         {  
  24.             base.OnLoad();  
  25.         }  
  26.  
  27.         public string InOrOut  
  28.         {  
  29.             get { return inOrOut; }  
  30.             set  
  31.             {  
  32.                 inOrOut = value;  
  33.                 InvokePropertyChanged(new PropertyChangedEventArgs("InOrOut"));  
  34.             }  
  35.         }  
  36.  
  37.         public string Time  
  38.         {  
  39.             get { return time; }  
  40.             set  
  41.             {  
  42.                 time = value;  
  43.                 InvokePropertyChanged(new PropertyChangedEventArgs("Time"));  
  44.             }  
  45.         }  
  46.  
  47.         public string Money  
  48.         {  
  49.             get { return money; }  
  50.             set  
  51.             {  
  52.                 money = value;  
  53.                 InvokePropertyChanged(new PropertyChangedEventArgs("Money"));  
  54.             }  
  55.         }  
  56.  
  57.         public string Description  
  58.         {  
  59.             get { return description; }  
  60.             set  
  61.             {  
  62.                 this.description = value;  
  63.                 InvokePropertyChanged(new PropertyChangedEventArgs("Description"));  
  64.             }  
  65.         }  
  66.  
  67.         #region INotifyPropertyChanged Members  
  68.  
  69.         public event PropertyChangedEventHandler PropertyChanged;  
  70.  
  71.         #endregion  
  72.  
  73.         //删除对象的数据  
  74.         public override void Deallocate()  
  75.         {  
  76.             base.Deallocate();  
  77.         }  
  78.  
  79.         private void InvokePropertyChanged(PropertyChangedEventArgs e)  
  80.         {  
  81.             var handler = PropertyChanged;  
  82.             if (handler != null) handler(this, e);  
  83.         }  
  84.     }  

AccountsViewModel.cs

 

 
  1. using System;  
  2. using System.Net;  
  3. using System.Windows;  
  4. using System.Windows.Controls;  
  5. using System.Windows.Documents;  
  6. using System.Windows.Ink;  
  7. using System.Windows.Input;  
  8. using System.Windows.Media;  
  9. using System.Windows.Media.Animation;  
  10. using System.Windows.Shapes;  
  11. using System.ComponentModel;  
  12. using System.Collections.ObjectModel;  
  13. using Perst;  
  14.  
  15. namespace PerstDemo.ViewModel  
  16. {  
  17.     public class AccountsViewModel : INotifyPropertyChanged  
  18.     {  
  19.         public AccountsViewModel()  
  20.         {  
  21.             Accounts = new ObservableCollection<Account>();  
  22.  
  23.             //从数据库中获取所有的Account记录  
  24.             if (Database != null)  
  25.             {  
  26.                 //数据库查询 查询出Account类(相当于表)的所有对象  通过时间进行排序  
  27.                 Accounts = Database.Select<Account>("order by Time").ToObservableCollection(); // Load them but sorted    
  28.             }  
  29.         }  
  30.  
  31.         public ObservableCollection<Account> Accounts { get; private set; }  
  32.  
  33.         private static Database Database  
  34.         {  
  35.             get { return ((App)Application.Current).Database; }  
  36.         }  
  37.  
  38.         private static Storage Storage  
  39.         {  
  40.             get { return Database.Storage; }  
  41.         }  
  42.  
  43.         public event PropertyChangedEventHandler PropertyChanged;  
  44.         private void NotifyPropertyChanged(String propertyName)  
  45.         {  
  46.             if (null != PropertyChanged)  
  47.             {  
  48.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
  49.             }  
  50.         }  
  51.  
  52.     }  

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

 

 
  1. View Code   
  2.  
  3. <phone:PhoneApplicationPage   
  4.     x:Class="PerstDemo.MainPage" 
  5.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  6.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  7.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  8.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  9.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  10.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  11.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696" 
  12.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  13.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  14.     Foreground="{StaticResource PhoneForegroundBrush}" 
  15.     SupportedOrientations="Portrait" Orientation="Portrait" 
  16.     shell:SystemTray.IsVisible="True" 
  17.     xmlns:vm="clr-namespace:PerstDemo.ViewModel" 
  18. > 
  19.  
  20.  
  21.  
  22.             <!--LayoutRoot is the root grid where all page content is placed--> 
  23.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  24.         <Grid.RowDefinitions> 
  25.             <RowDefinition Height="Auto"/> 
  26.             <RowDefinition Height="*"/> 
  27.         </Grid.RowDefinitions> 
  28.  
  29.         <!--TitlePanel contains the name of the application and page title--> 
  30.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  31.             <TextBlock x:Name="PageTitle" Text="流水账" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  32.         </StackPanel> 
  33.  
  34.         <!--ContentPanel - place additional content here--> 
  35.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  36.             <ListBox x:Name="AccountsListBox" Grid.Row="1"  > 
  37.                 <ListBox.ItemTemplate> 
  38.                     <DataTemplate> 
  39.                         <StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal"> 
  40.                             <StackPanel> 
  41.                                 <StackPanel Orientation="Horizontal"> 
  42.                                     <TextBlock x:Name="inout" Text="{Binding InOrOut}" Margin="0,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
  43.                                     <TextBlock x:Name="money" Text="{Binding Money}" Margin="10,0,0,5" VerticalAlignment="Bottom"  Style="{StaticResource PhoneTextNormalStyle}"/> 
  44.                                     <TextBlock x:Name="time" Text="{Binding Time}" Margin="70,0,0,0" VerticalAlignment="Bottom"  Style="{StaticResource PhoneTextNormalStyle}"/> 
  45.                                 </StackPanel> 
  46.                                 <TextBlock x:Name="DetailsText" Text="{Binding Description}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextAccentStyle}"/> 
  47.                             </StackPanel> 
  48.                         </StackPanel> 
  49.                     </DataTemplate> 
  50.                 </ListBox.ItemTemplate> 
  51.             </ListBox> 
  52.         </Grid> 
  53.     </Grid> 
  54.     <phone:PhoneApplicationPage.ApplicationBar> 
  55.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 
  56.             <shell:ApplicationBar.MenuItems> 
  57.                 <shell:ApplicationBarMenuItem Text="记一笔账" Click="New_Click"/> 
  58.             </shell:ApplicationBar.MenuItems> 
  59.         </shell:ApplicationBar> 
  60.     </phone:PhoneApplicationPage.ApplicationBar> 
  61.  
  62.  
  63. </phone:PhoneApplicationPage> 

 

 
  1. View Code   
  2.  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Net;  
  7. using System.Windows;  
  8. using System.Windows.Controls;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Animation;  
  13. using System.Windows.Shapes;  
  14. using Microsoft.Phone.Controls;  
  15. using PerstDemo.ViewModel;  
  16. using Perst;  
  17. using System.Collections.ObjectModel;  
  18.  
  19. namespace PerstDemo  
  20. {  
  21.     public partial class MainPage : PhoneApplicationPage  
  22.     {  
  23.         // Constructor  
  24.         public MainPage()  
  25.         {  
  26.             InitializeComponent();  
  27.             this.AccountsListBox.ItemsSource =  new AccountsViewModel().Accounts;  
  28.         }  
  29.  
  30.         private void New_Click(object sender, EventArgs e)  
  31.         {  
  32.             NavigationService.Navigate(new Uri("/AddAccount.xaml", UriKind.Relative));  
  33.         }  
  34.     }  

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

AddAccount.xaml

 

 
  1. View Code   
  2.  
  3. <phone:PhoneApplicationPage   
  4.     x:Class="PerstDemo.AddAccount" 
  5.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  6.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  7.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
  8.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
  9.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  10.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  11.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  12.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  13.     Foreground="{StaticResource PhoneForegroundBrush}" 
  14.     SupportedOrientations="Portrait" Orientation="Portrait" 
  15.     mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" 
  16.     shell:SystemTray.IsVisible="True"> 
  17.  
  18.     <!--LayoutRoot is the root grid where all page content is placed--> 
  19.     <Grid x:Name="LayoutRoot" Background="Transparent"> 
  20.         <Grid.RowDefinitions> 
  21.             <RowDefinition Height="Auto"/> 
  22.             <RowDefinition Height="*"/> 
  23.         </Grid.RowDefinitions> 
  24.  
  25.         <!--TitlePanel contains the name of the application and page title--> 
  26.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  27.             <TextBlock x:Name="PageTitle" Text="记账" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
  28.         </StackPanel> 
  29.  
  30.         <!--ContentPanel - place additional content here--> 
  31.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  32.             <RadioButton Content="收入" Height="92" HorizontalAlignment="Left" Margin="62,29,0,0" Name="income" FontSize="30" VerticalAlignment="Top" /> 
  33.             <RadioButton Content="支出" Height="92" HorizontalAlignment="Left" Margin="256,29,0,0" Name="outgo" FontSize="30" VerticalAlignment="Top" /> 
  34.             <TextBlock Height="43" HorizontalAlignment="Left" Margin="23,169,0,0" Name="textBlock1" Text="金额" FontSize="30" VerticalAlignment="Top" Width="100" /> 
  35.             <TextBox Height="72" HorizontalAlignment="Left" Margin="115,152,0,0" Name="money" Text="" VerticalAlignment="Top" Width="312" /> 
  36.             <TextBlock FontSize="30" Height="43" HorizontalAlignment="Left" Margin="19,250,0,0" Name="textBlock2" Text="备注" VerticalAlignment="Top" Width="100" /> 
  37.             <TextBox Height="72" HorizontalAlignment="Left" Margin="115,234,0,0" Name="desc" Text="" VerticalAlignment="Top" Width="312" /> 
  38.             <TextBlock FontSize="30" Height="43" HorizontalAlignment="Left" Margin="19,344,0,0" Name="textBlock3" Text="时间" VerticalAlignment="Top" Width="100" /> 
  39.             <TextBox Height="72" HorizontalAlignment="Left" Margin="115,330,0,0" Name="time" Text="" VerticalAlignment="Top" Width="312" /> 
  40.             <Button  FontSize="40" Content="保存" Height="111" HorizontalAlignment="Left" Margin="43,466,0,0" Name="button1" VerticalAlignment="Top" Width="384" Click="button1_Click" /> 
  41.         </Grid> 
  42.     </Grid> 
  43.  
  44. </phone:PhoneApplicationPage> 

 

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. using Perst;  
  14. using PerstDemo.ViewModel;  
  15.  
  16. namespace PerstDemo  
  17. {  
  18.     public partial class AddAccount : PhoneApplicationPage  
  19.     {  
  20.         public AddAccount()  
  21.         {  
  22.             InitializeComponent();  
  23.         }  
  24. //保存记录  
  25.         private void button1_Click(object sender, RoutedEventArgs e)  
  26.         {  
  27.             Database Database = ((App)App.Current).Database;//获取在App中定义的数据库对象  
  28.             string inorout = "支出";  
  29.             if (this.income.IsChecked != null && this.income.IsChecked == true)  
  30.             {  
  31.                inorout = "收入";  
  32.             }  
  33.             //初始化一个表对象  
  34.             Account tem1 = new Account { InOrOut = inoroutMoney = this.money.Text, Description = this.desc.Text,Time=this.time.Text };  
  35.             Database.AddRecord(tem1);//添加数据库记录  
  36.             Database.Storage.Commit();//关闭数据库存储  
  37.             NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));//跳转到首页的流水账显示  
  38.         }  
  39.     }  

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

Utilities.cs

 

 
  1. using System;  
  2. using System.Net;  
  3. using System.Windows;  
  4. using System.Windows.Controls;  
  5. using System.Windows.Documents;  
  6. using System.Windows.Ink;  
  7. using System.Windows.Input;  
  8. using System.Windows.Media;  
  9. using System.Windows.Media.Animation;  
  10. using System.Windows.Shapes;  
  11. using System.Collections.ObjectModel;  
  12. using System.Collections.Generic;  
  13.  
  14. namespace PerstDemo  
  15. {  
  16.     public static class Utilities  
  17.     {   //将IEnumerable<T>转化为ObservableCollection<T>   用于数据绑定  
  18.         public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> source)  
  19.         {  
  20.             var collection = new ObservableCollection<T>();  
  21.             foreach (var acount in source)  
  22.                 collection.Add(acount);  
  23.             return collection;  
  24.         }  
  25.     }  

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078622


相关文章
|
3月前
|
关系型数据库 MySQL 数据库
Windows安装MySQL数据库
本文介绍如何在Windows安装MySQL数据库。
68 0
|
1月前
|
NoSQL 网络协议 MongoDB
Windows公网远程连接MongoDB数据库【无公网IP】
Windows公网远程连接MongoDB数据库【无公网IP】
|
1月前
|
前端开发 Java Maven
java集成opencv(不踩坑),实现人脸检测小demo(含上传人像图片识别接口),windows,IDEA,Springboot
java集成opencv(不踩坑),实现人脸检测小demo(含上传人像图片识别接口),windows,IDEA,Springboot
186 0
|
2月前
|
Java 数据库连接 数据库
Windows7 64位 连接Access数据库“未发现数据源名称并且未指定默认驱动程序“的解决办法
Windows7 64位 连接Access数据库“未发现数据源名称并且未指定默认驱动程序“的解决办法
|
2月前
|
关系型数据库 MySQL Linux
MySQL 数据库安装详解(linux系统和windows系统)
MySQL 数据库是一种广泛使用的开源关系数据库管理系统。在 Linux 和 Windows 系统上安装 MySQL 数据库的步骤略有不同。
98 0
|
2月前
|
NoSQL 关系型数据库 MySQL
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
不同系统下进行MySQL安装、MongoDB安装、Redis安装【2月更文挑战第5天】
445 5
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
|
3月前
|
存储 JSON NoSQL
【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)
【1月更文挑战第26天】【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)
|
3月前
|
存储 NoSQL MongoDB
第4期 MongoDB数据库安装与启动(Windows)
第4期 MongoDB数据库安装与启动(Windows)
181 0
|
4月前
|
Windows
windows上先简单使用libevent,运行demo
windows上先简单使用libevent,运行demo
38 0