在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;
}
}
}

Windows Phone 7 使用Perst数据库的Demo——流水账相关推荐

  1. Silverlight实用窍门系列:74.Silverlight使用Perst数据库Demo

    Perst是一个简单.快速.便捷的面向Java和.Net的数据库.它可以直接将.net对象存储,在Silverlight不需要web service的方式进行读写,而是直接读写. 本文将编写一个实例进 ...

  2. 【Java】Java连接Mysql数据库的demo示例

    [Java]Java连接Mysql数据库的demo示例 1.安装mysql数据库 2.下载java-mysql-connector.jar包 3.完成java配置 4.写java代码运行测试 1.安装 ...

  3. 导出Windows服务器下的Oracle数据库并导入到Linux服务器下的Oracle数据库中

    2019独角兽企业重金招聘Python工程师标准>>> 说明: 1.Windows Oracle数据库 操作系统:Windows Server 2008 R2 IP地址:192.16 ...

  4. 编写一个Windows服务程序,定时从数据库中拿出记录发送邮件

    前言:编写一个Windows服务程序,定时从数据库中拿出记录发送邮件. 测试环境:Visual Studio 2005 SP1.Windows Server 2003 SP2 一.新建项目 打开VS2 ...

  5. postgresql主从备份_基于windows平台的postgresql主从数据库流备份配置

    基于windows平台的postgresql主从数据库流备份配置 因工作需要,需要搞pg数据库的主从备份,领导给了个方向使用流备份,于是开始朝着这个方向进发. 鸣谢大佬A_ccelerator的博客 ...

  6. Windows下自动备份Oracle数据库

    Windows下自动备份Oracle数据库 先说说为啥要搞这么个玩意 那是上线前几天[这不是讲故事],测试环境用的数据库崩了[为啥崩了不知道].之前造的一堆测试数据全都没得了[].然后急急忙忙的恢复环 ...

  7. 在Linux系统中访问虚拟机的数据库和访问Windows(本机)下的数据库:

    目录 访问虚拟机的数据库 访问Windows(本机)下的数据库: 他们两个访问的网页地址都是一致的都是Linux系统的地址 访问虚拟机的数据库 更改druid.properties 配置文件地址 地址 ...

  8. mysql在linux和windows下导入和导出数据库、数据表总结

    windows下 1.导出整个数据库 (常用) mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u dbuser -p dbname > dbna ...

  9. 阿里云WINDOWS SERVER 2019服务器安装MySQL数据库及设置远程访问权限教程

    本文详细介绍了MySQL数据库以下内容: (1).在阿里云WINDOWS SERVER 2019上安装MySQL数据库系统         (2).给MySQL数据库配置环境变量         (3 ...

  10. SQL Server研习录(24)——Windows Server 2012 R2安装数据库时提示KB2919355安装问题解决

    SQL Server研习录(24)--Windows Server 2012 R2安装数据库时提示KB2919355安装问题解决 版权声明 一.问题描述 二.解决办法 版权声明 本文原创作者:清风不渡 ...

最新文章

  1. 她取代马斯克成特斯拉新董事长 究竟什么来头?
  2. python是什么时候发布的_python发布日期
  3. rabbitmq生产者基于事务实现发送确认
  4. 优酷视频手机上能发现投屏设备,但投屏失败?
  5. APMServ5.2.6 升级php5.2 到 5.3版本及Memcache升级!
  6. 卷积,DFT,FFT,图像FFT,FIR 和 IIR 的物理意义。
  7. 数据库操作之——约束
  8. 基于springboot的美食系统
  9. JFrame+JButton简单使用(菜鸟入门)——JAVA
  10. 解决udhcpc命令无法自动获取并设置网卡IP和系统DNS
  11. 【Python】UnicodeDecodeError: 'gbk' codec can't decode byte 0xfe
  12. Error:Undefined symbol DMA_Cmd (referred from dac.o)
  13. 暗月渗透实战靶场-项目七(上)
  14. anguarjs 上传图片预览_前端战五渣学前端——FileReader预览本地文件
  15. redis 附近的人_Redis怎么实现查找附近的人,请看特殊数据类型Geospatial
  16. python 应用程序无法正常启动 000007b_“应用程序无法正常启动(oxc000007b)”解决方案...
  17. VS2015安装完成后Visual C++的一些模板找不到,安装C++新模板
  18. 局域网虚拟机服务器共享,两个虚拟机如何在局域网中共享
  19. Python居然能破解传说中的摩斯密码?“有内鬼,终止交易”
  20. 严选 | ELK Stack 选书指南

热门文章

  1. Centos7.0升级python 2.x到3.x
  2. Bengio最新博文:深度学习展望
  3. Caused by: org.hibernate.HibernateException: unknown Oracle major version [0]
  4. Python 学习笔记 - Redis
  5. sys.dm_db_wait_stats
  6. spring boot 配置启动后执行sql, 中文乱码
  7. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks 水题
  8. 包含min函数的栈 【微软面试100题 第二题】
  9. js如何判断一个数组中是否有重复的值
  10. paip.微信菜单直接跳转url和获取openid流程总结