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记录
if (Database != null)
{
//数据库查询 查询出Account类(相当于表)的所有对象 通过时间进行排序
Accounts = Database.Select("order by Time").ToObservableCollection(); // Load them but sorted
}
}
public ObservableCollection 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
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
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类型 数据绑定使用了ObservableCollection类型 所用需要转换一下
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转化为ObservableCollection 用于数据绑定
public static ObservableCollection ToObservableCollection(this IEnumerable source)
{
var collection = new ObservableCollection();
foreach (var acount in source)
collection.Add(acount);
return collection;
}
}
}