设为首页 收藏本站
查看: 2067|回复: 0

Windows Phone 7 使用Perst数据库的Demo——流水账

[复制链接]

尚未签到

发表于 2015-5-8 10:49:20 | 显示全部楼层 |阅读模式
  在Windows Phone 7程序项目中使用Perst,需要引用PerstWP7.dll,dll文件可以到Perst的官方网站上下载。这个perst数据库的demo简单地实现了记账保存功能和流水账查询的功能,旨在用最简单最简洁的代码在Windows Phone 7上使用Perst数据库。
  程序截图如下:
DSC0000.jpg DSC0001.jpg
  先从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记录
            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表的所有记录


DSC0002.gif DSC0003.gif 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;
        }
    }
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-64917-1-1.html 上篇帖子: IE9 与Windows 7 无缝集成 下篇帖子: socket实现Windows Phone 7即时聊天
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表