微軟WP7本地數(shù)據(jù)庫之Sterling編程技巧
Sterling是一款開源的嵌入式數(shù)據(jù)庫軟件。在本篇中,我們將分析如何把Sterling數(shù)據(jù)庫集成到Windows Phone 7程序中及相應的編程技巧和注意事項。
一、創(chuàng)建MainViewModel
為了實現(xiàn)更加模塊化的設(shè)計,我們將創(chuàng)建一個視圖模型,用以封裝上篇中定義的兩個數(shù)據(jù)庫表格。為簡單起見,我們主要提供了數(shù)據(jù)加載方面的支持。使用MainViewModel的另一個原因是下載的示例程序中正是使用了這種系統(tǒng)架構(gòu)方式。然而,請注意這個例子中并沒有利用流行的MVVM設(shè)計模式。
列表1:定義主視圖模型實現(xiàn)與Sterling數(shù)據(jù)庫層的關(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。在這個方法中,我們首先判斷這兩個表GroupViewModel和ContactViewModel是否已建立。如果已經(jīng)建立,我們只需要用簡單的Sterling查詢操作填充兩個集合;否則,我們調(diào)用另一個方法_SetupData生成新表中的記錄。創(chuàng)建記錄簡單地對應于創(chuàng)建相關(guān)類的實例。***,我們設(shè)置全局變量IsDataLoaded的值以方便隨后的判斷之用。
到目前為止,所有數(shù)據(jù)層相關(guān)編程已經(jīng)完成。接下來,我們將介紹如何把Sterling集成到Windows Phone7應用程序中。
#p#
二、把Sterling集成到WP7項目中
現(xiàn)在,我們已經(jīng)定義了一個Sterling數(shù)據(jù)庫、兩個表及相應的索引。那么,接下來我們來分析如何把Sterling數(shù)據(jù)庫集成到我們的示例應用程序WP7SterlingLearning中。請注意,WP7SterlingLearning僅是一個普通的Windows Phone7應用程序,沒有什么特別之處。從總體上看,關(guān)鍵的問題主要發(fā)生在文件App.xaml.cs中。
1.添加所需的組件
若要使用Sterling數(shù)據(jù)庫,首先要添加使用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的定義有助于在應用程序的各處引用這一數(shù)據(jù)庫。此外,另一個全局靜態(tài)屬性ViewModel也起著類似的作用。
其次,我們定義了兩個輔助方法。***個方法_ActivateEngine旨在當程序***啟動時或者當程序從墓碑(tombstone)事件中喚醒時激活數(shù)據(jù)庫ContactsDatabase。第二個方法_DeactivateEngine用于當應用程序退出或進入到墓碑(tombstone)事件中時停用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;
- }
這里有幾點值得注意。首先,我們使用內(nèi)置的Sterling日志記錄器簡單地把執(zhí)行結(jié)果輸出到調(diào)試窗口。事實上,Sterling也支持編寫自定義日志記錄器并注冊到Sterling引擎。然而,有關(guān)編寫自定義日志記錄器的問題已經(jīng)超出了本文的范圍。其次,通過調(diào)用方法RegisterDatabase我們把數(shù)據(jù)庫注冊到Sterling引擎中。第三,我們把兩個先前定義的觸發(fā)器通過數(shù)據(jù)庫的RegisterTrigger方法注冊到Sterling引擎。
2.在WP7生命周期事件中控制Sterling引擎
現(xiàn)在,我們可以使用Windows Phone 7應用程序的四個典型的生命周期事件的編程來調(diào)用上面的兩個輔助方法。
列表4:在WP7應用程序生命周期事件中控制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應用程序的上述四個生命周期事件的解釋,請參考其他的Windows Phone 7入門性文章,在此不再贅述。
截至目前,Sterling數(shù)據(jù)庫大部分的相關(guān)工作已經(jīng)完成。隨后的任務是把Sterling數(shù)據(jù)庫結(jié)合進WP7視圖頁面的編程中。
#p#
三、編寫視圖頁面
現(xiàn)在,讓我們來看看如何編寫兩個示例網(wǎng)頁。首先,***個頁面MainPage.xaml的構(gòu)建非常簡單,僅僅使用一個ListBox控件來顯示組數(shù)據(jù)列表。
列表5: 主視圖頁面XAML關(guān)鍵標記代碼
- <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)驗的讀者應當都會理解上面的標記代碼。這里通過使用基本的數(shù)據(jù)綁定技術(shù)(我們使用ListBox)來渲染GroupViewModel實例的每一個屬性值。
接下來,讓我們看看主視圖頁面后臺代碼編寫的情況。
列表6:主視圖頁面后臺關(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。這一點很重要,也展示了整個系統(tǒng)的用戶描述層是如何實現(xiàn)的。
接下來,讓我們來分析一下MainPage_Loaded方法-每次示例頁面加載時都調(diào)用這個方法。預先檢查一下是否有必要再次加載視圖模型數(shù)據(jù)是一個很好的設(shè)計習慣。
***一點值得注意的是ListBox控件的SelectionChanged事件處理程序。在這個方法中,我們首先判斷用戶是否點擊了ListBox控件中的項目之一。如果沒有選中某個項目,即可返回;否則,我們把當前用戶導航到另一個網(wǎng)頁ContactList.xaml,以顯示對應于所選組對應的聯(lián)系人信息。注意,在頁面ContactList.xaml的OnNavigatedTo事件處理器中我們傳遞進參數(shù)selectedGroup。
至于頁面ContactList.xaml,有點復雜,但仍然很容易理解。首先,讓我們來看一下這個頁面的XAML標記代碼的情況。
列表7: 頁面ContactList.xaml的XAML標記代碼
- <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>
為了簡單起見,我們還利用一個ListBox控件來展示聯(lián)系人信息。同時,我們也使用了典型的Silverlight數(shù)據(jù)綁定技術(shù)。總體來看,上面代碼中只有一點值得注意,即圖像數(shù)據(jù)的顯示問題。為此,代碼中使用了一個自定義的類型轉(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();
- }
- }
注意,在本示例實現(xiàn)方案中,在構(gòu)建聯(lián)系人Contact的實例時,我們只持有在ThumbNail屬性中存儲的聯(lián)系人相關(guān)縮略圖的路徑信息。上面轉(zhuǎn)換器的關(guān)鍵在于Convert方法。在這個方法中,我們通過圖片路徑返回對應于縮略圖的BitmapImage實例。然后,我們將它設(shè)置為圖像控件的Source屬性。
至于頁面ContactList.xaml后臺代碼的實現(xiàn)就很簡單了,如下所示。
列表9: 頁面ContactList.xaml后臺代碼的實現(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中的基本導航方法,不必再贅述了。在上面代碼中,一旦我們接收下selectedGroup參數(shù),我們即使用它來創(chuàng)建一個標準的Sterling查詢語句。注意,要想全面領(lǐng)會這里的查詢語句,你應該先知道LazyValue在Sterling使用中所起的重要作用。***,我們把查詢結(jié)果綁定到ListBox控件的ItemsSource屬性。這就是全部!
#p#
四、總結(jié)
在本系列文章中,我們只是初步介紹了Sterling這款開源的面向?qū)ο笄度胧綌?shù)據(jù)庫在Silverlight for Windows Phone 7中的基本應用思路和操作技巧。很顯然,如果讀者以前未曾有過面向?qū)ο髷?shù)據(jù)庫方面的知識,你會感覺到Sterling的實現(xiàn)架構(gòu)有些陌生。然而,如果您已經(jīng)有扎實的C#面向?qū)ο蠛蚅INQ to Object基礎(chǔ),那么,快速入門Sterling也不是什么難事。當然,不同于傳統(tǒng)的關(guān)系數(shù)據(jù)庫,在使用Sterling前,你***預先掌握一些面向?qū)ο蟪绦蛟O(shè)計技術(shù),特別是掌握一些面向?qū)ο蟮臄?shù)據(jù)庫理論。隨著Sterling在你心中不斷扎根,你會發(fā)現(xiàn)Sterling是一個相當不錯的工具-輕量級而且高效率,非常適合Windows Phone 7數(shù)據(jù)驅(qū)動應用程序的開發(fā)。實際上,本文介紹的只不過是觸及了Sterling的一點皮毛,還有更多更好的東西等著您自己去深入挖掘。
【編輯推薦】
- 為您介紹幾款開源的數(shù)據(jù)挖掘工具
- 告訴你如何解決MySQL server has gone away問題
- 數(shù)據(jù)庫中分組字符串相加
- SQL點滴之收集SQL Server線程等待信息
- 數(shù)據(jù)庫的性能已成重多廠商關(guān)注的焦點