日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Windows Phone中使用Local DataBase与ISolateStorage—在MVVM模式下(—)

發布時間:2025/7/14 windows 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows Phone中使用Local DataBase与ISolateStorage—在MVVM模式下(—) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

????? 像我們知道的一樣,Windows Phone支持ISolateStorage,Mango中還新增了使用Linq的SQL CE作為本地數據庫。下面我們就用MVVMLight來模擬個消息發送和查看消息歷史記錄的IM簡易聊天窗口,來看看WP7是怎么使用ISolateStorage和Local DataBase的:

一.新建MVVMLight的Windows Phone Project

????? MVVMLight的教程可以在園子里搜一下,或者去MVVMLight作者的官方網站。建完Project后,MVVMLight的Windows Phone 模板已經自動生成了MainPage.xaml,以及一個MainViewModel。我們在Project中添加View文件夾,新建兩個View,即兩個PhoneApplicationPage,作為發送Message窗口和查看Message History記錄。還有新建一個ViewModel(MessageViewModel),Model中新增一個HaisaDataBase,用來管理我們的LocalDataBase。本來想定義一個ISolateStorageManager的Helper類,但是微軟已經把ISolateStorage封裝的夠簡單了,所以此處就省略了。

????? 添加后如下圖示:

可以看到圖中工程中有個icons的文件夾,是用來保存PhoneApplicationPage.ApplicationBar的圖標文件。

二.在Model中建立SQL CE數據庫

????? 我們新建個UserInfoTable,使其繼承INotifyPropertyChanged,INotifyPropertyChanging接口,并對其聲明[Table] Attribute。在UserInfoTable表中聲明幾個屬性作為Table的Column,在屬性前面加上[Column] Attribute。包括

UserId、UserNameAndTime、UserPwd、UserMessage。代碼類似下圖: 1: [Table] 2: ublic class UserInfoTable:INotifyPropertyChanged,INotifyPropertyChanging 3: 4: #region UserId Column 5: private int _userId; 6: [Column(IsPrimaryKey=true,IsDbGenerated=true,DbType="INT Not null identity",CanBeNull=false,AutoSync=AutoSync.OnInsert)] 7: public int UserId 8: { 9: get{return _userId; } 10: set{ 11: if(value==null) return; 12: 13: NotifyPropertyChanging("UserId"); 14: _userId=value; 15: NotifyPropertyChanged("UserId"); 16: 17: } 18: #endregion 19: ......... 20: 21: }

????? 在Windows Phone中,即可用UserInfoTable Class來生成數據庫中的UserInfoTable表,當然要記得引用下面幾個命名空間

using System.Data.Linq; using System.Data.Linq.Mapping; using Microsoft.Phone.Data.Linq; using Microsoft.Phone.Data.Linq.Mapping;

????? 我們再新建一個HaisaDataContext類繼承DataContext,構造函數繼承DataContext的構造函數,在其中添加我們的ConnectionString,還有聲明我們的Table,如下面代碼:

1: public class HaisaDataContext:DataContext 2: { 3: public static string DBConnectionString = "Data Source=isostore:/Haisa_DataBase.sdf"; 4: public HaisaDataContext(string connectionString):base(connectionString){} 5: public Table<UserInfoTable> HaisaTable; 6: 7: }

到這里我們的LocalDataBase就建立完了,關于Local DataBase的更多信息請查看MSDN

三.建立MessageView.xaml界面以及綁定MessageViewModel

????? 我們在MainPage.xaml中添加一個HyperlinkButton,設置NavigateUri="/View/MessageView.xaml" 。

然后我們開始設計MessageView.xaml,用VS或者Blend在MessageView.xaml中設計類似聊天工具的界面時,我們先加入一個Listbox來顯示當前發送的Message列表,然后添加一個消息輸入框和Send Message按鈕。如圖示:

?

????? 第二個圖中可以看到一個ApplicationBar,這個ApplicationBar用來查看消息歷史記錄,導航至新的Message History Page。

ApplicationBar的添加類似下面:

1: <phone:PhoneApplicationPage.ApplicationBar> 2: <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 3: <shell:ApplicationBar.MenuItems> 4: <shell:ApplicationBarMenuItem x:Name="menuItem_History" Text="History"/> 5: </shell:ApplicationBar.MenuItems> 6: <shell:ApplicationBarIconButton x:Name="appbar_HistoryButton" 7: IconUri="/icons/appbar.folder.rest.png" 8: Text="Message History" 9: Click="appbar_HistoryButton_Click"/> 10: </shell:ApplicationBar> 11: </phone:PhoneApplicationPage.ApplicationBar>

????? 其中ApplicationBarIconButton的appbar_HistoryButton_Click中調用NavigationService的Navigate方法進行導航。

后臺代碼如下:

1: private void appbar_HistoryButton_Click(object sender, System.EventArgs e) 2: { 3: this.NavigationService.Navigate(new Uri("/View/MessageHistoryView.xaml",UriKind.RelativeOrAbsolute)); 4: }

????? 第一步中我們已經建立了一個新的MessageViewModel,作為MessageView和MessageHistoryView公用的ViewModel,當然,實際建項目時這兩個最好分開,因為功能不一致,但是我們這里只共用一個ViewModel以簡化代碼。

我們在ViewModelLocator中注冊MessageViewModel:

1: public MessageViewModel MsgViewModel 2: { 3: get 4: { 5: return new MessageViewModel(); 6: } 7: 8: }

????? 這樣子我們就可以在MessageView和MessageHistoryView的xaml文件中通過這樣的方式在View中綁定ViewModel

DataContext="{Binding MsgViewModel,Source={StaticResource Locator}}"

????? 到這一步,基本框架的東西都已經搭好了,下一步就是開始添加SendMessage和ShowMessageHistory的功能,我們將在下一篇中繼續,

Coming Soon…….

轉載于:https://www.cnblogs.com/haisa/archive/2012/01/14/2322165.html

總結

以上是生活随笔為你收集整理的Windows Phone中使用Local DataBase与ISolateStorage—在MVVM模式下(—)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。