微軟WP7本地?cái)?shù)據(jù)庫(kù)之Sterling編程技巧
Sterling是一款開源的嵌入式數(shù)據(jù)庫(kù)軟件。在本篇中,我們將分析如何把Sterling數(shù)據(jù)庫(kù)集成到Windows Phone 7程序中及相應(yīng)的編程技巧和注意事項(xiàng)。
一、創(chuàng)建MainViewModel
為了實(shí)現(xiàn)更加模塊化的設(shè)計(jì),我們將創(chuàng)建一個(gè)視圖模型,用以封裝上篇中定義的兩個(gè)數(shù)據(jù)庫(kù)表格。為簡(jiǎn)單起見(jiàn),我們主要提供了數(shù)據(jù)加載方面的支持。使用MainViewModel的另一個(gè)原因是下載的示例程序中正是使用了這種系統(tǒng)架構(gòu)方式。然而,請(qǐng)注意這個(gè)例子中并沒(méi)有利用流行的MVVM設(shè)計(jì)模式。
列表1:定義主視圖模型實(shí)現(xiàn)與Sterling數(shù)據(jù)庫(kù)層的關(guān)聯(lián)
- public class MainViewModel : INotifyPropertyChanged
- {
- public MainViewModel()
- {
- this.Groups = new ObservableCollection();
- this.Contacts = new ObservableCollection();
- }
- public ObservableCollection Groups { get; private set; }
- public ObservableCollection Contacts { get; private set; }
- public bool IsDataLoaded
- {
- get;
- private set;
- }
- public void LoadData()
- {
- bool hasKeys = false;
- foreach (var item in App.Database.Query())
- {
- hasKeys = true;
- break;
- }
- bool hasKeys2 = false;
- foreach (var item in App.Database.Query())
- {
- hasKeys2 = true;
- break;
- }
- if (!hasKeys && !hasKeys2)
- {
- _SetupData();
- }
- foreach (var item in App.Database.Query())
- {
- Groups.Add(item.LazyValue.Value);
- }
- foreach (var item in App.Database.Query())
- {
- Contacts.Add(item.LazyValue.Value);
- }
- this.IsDataLoaded = true;
- }
- private void _SetupData()
- {
- var groupData = new List()
- {
- new GroupViewModel() { GroupName = "GP1"},
- //others omitted…
- new GroupViewModel() { GroupName = "GP10"}
- };
- foreach (var item in groupData)
- {
- App.Database.Save(item);
- }
- var contactData = new List()
- {
- new ContactViewModel() { GroupId=1,Name="Name11",Email="Name11@hotmail.com",ThumbNail="/WP7SterlingLearning;Component/ThumbNails/11.jpg"},
- new ContactViewModel() { GroupId=1,Name="Name12",Email="Name12@hotmail.com",ThumbNail="/WP7SterlingLearning;Component/ThumbNails/12.jpg"},
- //others omitted…
- new ContactViewModel() { GroupId=10,Name="Name105",Email="Name105@hotmail.com",ThumbNail="/WP7SterlingLearning;Component/ThumbNails/105.jpg"}
- };
- foreach (var item in contactData)
- {
- App.Database.Save(item);
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- private void NotifyPropertyChanged(String propertyName)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (null != handler)
- {
- handler(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
很容易看出,上面代碼的關(guān)鍵在于方法LoadData。在這個(gè)方法中,我們首先判斷這兩個(gè)表GroupViewModel和ContactViewModel是否已建立。如果已經(jīng)建立,我們只需要用簡(jiǎn)單的Sterling查詢操作填充兩個(gè)集合;否則,我們調(diào)用另一個(gè)方法_SetupData生成新表中的記錄。創(chuàng)建記錄簡(jiǎn)單地對(duì)應(yīng)于創(chuàng)建相關(guān)類的實(shí)例。***,我們?cè)O(shè)置全局變量IsDataLoaded的值以方便隨后的判斷之用。
到目前為止,所有數(shù)據(jù)層相關(guān)編程已經(jīng)完成。接下來(lái),我們將介紹如何把Sterling集成到Windows Phone7應(yīng)用程序中。
#p#
二、把Sterling集成到WP7項(xiàng)目中
現(xiàn)在,我們已經(jīng)定義了一個(gè)Sterling數(shù)據(jù)庫(kù)、兩個(gè)表及相應(yīng)的索引。那么,接下來(lái)我們來(lái)分析如何把Sterling數(shù)據(jù)庫(kù)集成到我們的示例應(yīng)用程序WP7SterlingLearning中。請(qǐng)注意,WP7SterlingLearning僅是一個(gè)普通的Windows Phone7應(yīng)用程序,沒(méi)有什么特別之處。從總體上看,關(guān)鍵的問(wèn)題主要發(fā)生在文件App.xaml.cs中。
1.添加所需的組件
若要使用Sterling數(shù)據(jù)庫(kù),首先要添加使用Sterling引擎所需的組件。下面給出關(guān)鍵代碼部分:
列表2:添加使用Sterling引擎所需的組件的關(guān)鍵代碼
- //省略其他引用…
- using Wintellect.Sterling;
- using WP7SterlingLearning.Database;
- namespace WP7SterlingLearning
- {
- public partial class App : Application
- {
- private static MainViewModel viewModel = null;
- private static ISterlingDatabaseInstance _database = null;
- private static SterlingEngine _engine = null;
- private static SterlingDefaultLogger _logger = null;
- //The MainViewModel object
- public static MainViewModel ViewModel
- {
- get
- {
- if (viewModel == null)
- viewModel = new MainViewModel();
- return viewModel;
- }
- }
- public static ISterlingDatabaseInstance Database
- {
- get
- {
- return _database;
- }
- }
- //…
注意,上面的全局靜態(tài)屬性Database的定義有助于在應(yīng)用程序的各處引用這一數(shù)據(jù)庫(kù)。此外,另一個(gè)全局靜態(tài)屬性ViewModel也起著類似的作用。
其次,我們定義了兩個(gè)輔助方法。***個(gè)方法_ActivateEngine旨在當(dāng)程序***啟動(dòng)時(shí)或者當(dāng)程序從墓碑(tombstone)事件中喚醒時(shí)激活數(shù)據(jù)庫(kù)ContactsDatabase。第二個(gè)方法_DeactivateEngine用于當(dāng)應(yīng)用程序退出或進(jìn)入到墓碑(tombstone)事件中時(shí)停用Sterling引擎。
列表3:方法_ActivateEngine的關(guān)鍵代碼部分
- private void _ActivateEngine()
- {
- _engine = new SterlingEngine();
- _logger = new SterlingDefaultLogger(SterlingLogLevel.Information);
- _engine.Activate();
- _database = _engine.SterlingDatabase.RegisterDatabase();
- //register triggers
- var maxIdx1 =
- _database.Query().Any() ?
- (from id in _database.Query()
- select id.Key).Max() + 1 : 1;
- _database.RegisterTrigger(new ContactsDatabase.GroupTrigger(maxIdx1));
- var maxIdx2 =
- _database.Query().Any() ?
- (from id in _database.Query()
- select id.Key).Max() + 1 : 1;
- _database.RegisterTrigger(new ContactsDatabase.ContactTrigger(maxIdx2));
- }
- private void _DeactivateEngine()
- {
- _logger.Detach();
- _engine.Dispose();
- _database = null;
- _engine = null;
- }
這里有幾點(diǎn)值得注意。首先,我們使用內(nèi)置的Sterling日志記錄器簡(jiǎn)單地把執(zhí)行結(jié)果輸出到調(diào)試窗口。事實(shí)上,Sterling也支持編寫自定義日志記錄器并注冊(cè)到Sterling引擎。然而,有關(guān)編寫自定義日志記錄器的問(wèn)題已經(jīng)超出了本文的范圍。其次,通過(guò)調(diào)用方法RegisterDatabase我們把數(shù)據(jù)庫(kù)注冊(cè)到Sterling引擎中。第三,我們把兩個(gè)先前定義的觸發(fā)器通過(guò)數(shù)據(jù)庫(kù)的RegisterTrigger方法注冊(cè)到Sterling引擎。
2.在WP7生命周期事件中控制Sterling引擎
現(xiàn)在,我們可以使用Windows Phone 7應(yīng)用程序的四個(gè)典型的生命周期事件的編程來(lái)調(diào)用上面的兩個(gè)輔助方法。
列表4:在WP7應(yīng)用程序生命周期事件中控制Sterling引擎
- private void Application_Launching(object sender, LaunchingEventArgs e)
- {
- _ActivateEngine();
- }
- private void Application_Activated(object sender, ActivatedEventArgs e)
- {
- _ActivateEngine();
- if (!App.ViewModel.IsDataLoaded)
- {
- App.ViewModel.LoadData();
- }
- }
- private void Application_Deactivated(object sender, DeactivatedEventArgs e)
- {
- _DeactivateEngine();
- }
- private void Application_Closing(object sender, ClosingEventArgs e)
- {
- _DeactivateEngine();
- }
有關(guān)Windows Phone 7應(yīng)用程序的上述四個(gè)生命周期事件的解釋,請(qǐng)參考其他的Windows Phone 7入門性文章,在此不再贅述。
截至目前,Sterling數(shù)據(jù)庫(kù)大部分的相關(guān)工作已經(jīng)完成。隨后的任務(wù)是把Sterling數(shù)據(jù)庫(kù)結(jié)合進(jìn)WP7視圖頁(yè)面的編程中。
#p#
三、編寫視圖頁(yè)面
現(xiàn)在,讓我們來(lái)看看如何編寫兩個(gè)示例網(wǎng)頁(yè)。首先,***個(gè)頁(yè)面MainPage.xaml的構(gòu)建非常簡(jiǎn)單,僅僅使用一個(gè)ListBox控件來(lái)顯示組數(shù)據(jù)列表。
列表5: 主視圖頁(yè)面XAML關(guān)鍵標(biāo)記代碼
- <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Groups}"
- SelectionChanged="MainListBox_SelectionChanged">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal">
- <TextBlock Text="{Binding GroupId}" TextWrapping="Wrap"
- Style="{StaticResource PhoneTextExtraLargeStyle}"/>
- <TextBlock Text="{Binding GroupName}" TextWrapping="Wrap" Margin="12,-
- 6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
現(xiàn)在,稍有Siverlight經(jīng)驗(yàn)的讀者應(yīng)當(dāng)都會(huì)理解上面的標(biāo)記代碼。這里通過(guò)使用基本的數(shù)據(jù)綁定技術(shù)(我們使用ListBox)來(lái)渲染GroupViewModel實(shí)例的每一個(gè)屬性值。
接下來(lái),讓我們看看主視圖頁(yè)面后臺(tái)代碼編寫的情況。
列表6:主視圖頁(yè)面后臺(tái)關(guān)鍵代碼
- namespace WP7SterlingLearning
- {
- public partial class MainPage : PhoneApplicationPage
- {
- public MainPage()
- {
- InitializeComponent();
- DataContext = App.ViewModel;
- this.Loaded += new RoutedEventHandler(MainPage_Loaded);
- }
- private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (MainListBox.SelectedIndex == -1)
- return;
- NavigationService.Navigate(new Uri("/ContactList.xaml?selectedGroup=" + MainListBox.SelectedIndex, UriKind.Relative));
- MainListBox.SelectedIndex = -1;
- }
- private void MainPage_Loaded(object sender, RoutedEventArgs e)
- {
- if (!App.ViewModel.IsDataLoaded)
- {
- App.ViewModel.LoadData();
- }
- }
- }
- }
首先,在MainPage類的構(gòu)造函數(shù)中,我們把DataContext屬性綁定到App.ViewModel。這一點(diǎn)很重要,也展示了整個(gè)系統(tǒng)的用戶描述層是如何實(shí)現(xiàn)的。
接下來(lái),讓我們來(lái)分析一下MainPage_Loaded方法-每次示例頁(yè)面加載時(shí)都調(diào)用這個(gè)方法。預(yù)先檢查一下是否有必要再次加載視圖模型數(shù)據(jù)是一個(gè)很好的設(shè)計(jì)習(xí)慣。
***一點(diǎn)值得注意的是ListBox控件的SelectionChanged事件處理程序。在這個(gè)方法中,我們首先判斷用戶是否點(diǎn)擊了ListBox控件中的項(xiàng)目之一。如果沒(méi)有選中某個(gè)項(xiàng)目,即可返回;否則,我們把當(dāng)前用戶導(dǎo)航到另一個(gè)網(wǎng)頁(yè)ContactList.xaml,以顯示對(duì)應(yīng)于所選組對(duì)應(yīng)的聯(lián)系人信息。注意,在頁(yè)面ContactList.xaml的OnNavigatedTo事件處理器中我們傳遞進(jìn)參數(shù)selectedGroup。
至于頁(yè)面ContactList.xaml,有點(diǎn)復(fù)雜,但仍然很容易理解。首先,讓我們來(lái)看一下這個(gè)頁(yè)面的XAML標(biāo)記代碼的情況。
列表7: 頁(yè)面ContactList.xaml的XAML標(biāo)記代碼
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <ListBox x:Name="MainListBox" Margin="0,0,-12,0" >
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Margin="0,0,0,17" Width="432">
- <TextBlock Text="{Binding GroupId}" TextWrapping="Wrap"
- Style="{StaticResource PhoneTextExtraLargeStyle}"/>
- <TextBlock Text="{Binding Id}" TextWrapping="Wrap" Margin="12,-6,12,0"
- Style="{StaticResource PhoneTextSubtleStyle}"/>
- <TextBlock Text="{Binding Name}" TextWrapping="Wrap"
- Style="{StaticResource PhoneTextExtraLargeStyle}"/>
- <TextBlock Text="{Binding Email}" TextWrapping="Wrap" Margin="12,-
- 6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
- <Image Source="{Binding ThumbNail,Converter={StaticResource
- ImageConverter1}}" />
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- </Grid>
為了簡(jiǎn)單起見(jiàn),我們還利用一個(gè)ListBox控件來(lái)展示聯(lián)系人信息。同時(shí),我們也使用了典型的Silverlight數(shù)據(jù)綁定技術(shù)??傮w來(lái)看,上面代碼中只有一點(diǎn)值得注意,即圖像數(shù)據(jù)的顯示問(wèn)題。為此,代碼中使用了一個(gè)自定義的類型轉(zhuǎn)換器。
列表8: 自定義的類型轉(zhuǎn)換器代碼
- public class imageUrlConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter,
- System.Globalization.CultureInfo culture)
- {
- if (value == null || parameter == null)
- {
- return value;
- }
- return new BitmapImage(new Uri((string)value, UriKind.RelativeOrAbsolute));
- }
- public object ConvertBack(object value, Type targetType, object parameter,
- System.Globalization.CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
注意,在本示例實(shí)現(xiàn)方案中,在構(gòu)建聯(lián)系人Contact的實(shí)例時(shí),我們只持有在ThumbNail屬性中存儲(chǔ)的聯(lián)系人相關(guān)縮略圖的路徑信息。上面轉(zhuǎn)換器的關(guān)鍵在于Convert方法。在這個(gè)方法中,我們通過(guò)圖片路徑返回對(duì)應(yīng)于縮略圖的BitmapImage實(shí)例。然后,我們將它設(shè)置為圖像控件的Source屬性。
至于頁(yè)面ContactList.xaml后臺(tái)代碼的實(shí)現(xiàn)就很簡(jiǎn)單了,如下所示。
列表9: 頁(yè)面ContactList.xaml后臺(tái)代碼的實(shí)現(xiàn)
- protected override void OnNavigatedTo(NavigationEventArgs e)
- {
- string selectedGroup = "";
- if (NavigationContext.QueryString.TryGetValue("selectedGroup", out selectedGroup))
- {
- int iGroup = int.Parse(selectedGroup);
- // Query from Database by key
- var contactList =from key in
- App.Database.Query().Where(
- x => x.LazyValue.Value.GroupId == iGroup + 1).ToList() select key.LazyValue.Value;
- MainListBox.ItemsSource=contactList;
- }
- }
OnNavigatedTo方法是Silverlight for WP7中的基本導(dǎo)航方法,不必再贅述了。在上面代碼中,一旦我們接收下selectedGroup參數(shù),我們即使用它來(lái)創(chuàng)建一個(gè)標(biāo)準(zhǔn)的Sterling查詢語(yǔ)句。注意,要想全面領(lǐng)會(huì)這里的查詢語(yǔ)句,你應(yīng)該先知道LazyValue在Sterling使用中所起的重要作用。***,我們把查詢結(jié)果綁定到ListBox控件的ItemsSource屬性。這就是全部!
#p#
四、總結(jié)
在本系列文章中,我們只是初步介紹了Sterling這款開源的面向?qū)ο笄度胧綌?shù)據(jù)庫(kù)在Silverlight for Windows Phone 7中的基本應(yīng)用思路和操作技巧。很顯然,如果讀者以前未曾有過(guò)面向?qū)ο髷?shù)據(jù)庫(kù)方面的知識(shí),你會(huì)感覺(jué)到Sterling的實(shí)現(xiàn)架構(gòu)有些陌生。然而,如果您已經(jīng)有扎實(shí)的C#面向?qū)ο蠛蚅INQ to Object基礎(chǔ),那么,快速入門Sterling也不是什么難事。當(dāng)然,不同于傳統(tǒng)的關(guān)系數(shù)據(jù)庫(kù),在使用Sterling前,你***預(yù)先掌握一些面向?qū)ο蟪绦蛟O(shè)計(jì)技術(shù),特別是掌握一些面向?qū)ο蟮臄?shù)據(jù)庫(kù)理論。隨著Sterling在你心中不斷扎根,你會(huì)發(fā)現(xiàn)Sterling是一個(gè)相當(dāng)不錯(cuò)的工具-輕量級(jí)而且高效率,非常適合Windows Phone 7數(shù)據(jù)驅(qū)動(dòng)應(yīng)用程序的開發(fā)。實(shí)際上,本文介紹的只不過(guò)是觸及了Sterling的一點(diǎn)皮毛,還有更多更好的東西等著您自己去深入挖掘。
【編輯推薦】
- 為您介紹幾款開源的數(shù)據(jù)挖掘工具
- 告訴你如何解決MySQL server has gone away問(wèn)題
- 數(shù)據(jù)庫(kù)中分組字符串相加
- SQL點(diǎn)滴之收集SQL Server線程等待信息
- 數(shù)據(jù)庫(kù)的性能已成重多廠商關(guān)注的焦點(diǎn)