成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

微軟WP7本地數(shù)據(jù)庫之Sterling編程技巧

數(shù)據(jù)庫 其他數(shù)據(jù)庫 數(shù)據(jù)庫運維
Sterling是一款開源的嵌入式數(shù)據(jù)庫軟件。在本篇中,我們將分析如何把Sterling數(shù)據(jù)庫集成到Windows Phone 7程序中及相應的編程技巧和注意事項。

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)

  1. public class MainViewModel : INotifyPropertyChanged  
  2. {  
  3.    public MainViewModel()  
  4.    {  
  5.       this.Groups = new ObservableCollection();  
  6.       this.Contacts = new ObservableCollection();  
  7.    }  
  8.    public ObservableCollection Groups { getprivate set; }  
  9.    public ObservableCollection Contacts { getprivate set; }  
  10.    public bool IsDataLoaded  
  11.    {  
  12.       get;  
  13.       private set;  
  14.    }  
  15.    public void LoadData()  
  16.    {  
  17.       bool hasKeys = false;  
  18.       foreach (var item in App.Database.Query())  
  19.       {   
  20.          hasKeys = true;  
  21.          break;  
  22.       }  
  23.       bool hasKeys2 = false;  
  24.       foreach (var item in App.Database.Query())  
  25.       {  
  26.          hasKeys2 = true;  
  27.          break;  
  28.       }  
  29.       if (!hasKeys && !hasKeys2)  
  30.       {  
  31.          _SetupData();  
  32.       }  
  33.       foreach (var item in App.Database.Query())  
  34.       {  
  35.          Groups.Add(item.LazyValue.Value);  
  36.       }  
  37.       foreach (var item in App.Database.Query())  
  38.       {  
  39.          Contacts.Add(item.LazyValue.Value);  
  40.       }  
  41.       this.IsDataLoaded = true;  
  42.    }  
  43.    private void _SetupData()  
  44.    {  
  45.       var groupData = new List()  
  46.       {  
  47.          new GroupViewModel() { GroupName = "GP1"},  
  48.          //others omitted…  
  49.          new GroupViewModel() { GroupName = "GP10"}  
  50.       };  
  51.       foreach (var item in groupData)  
  52.       {  
  53.          App.Database.Save(item);  
  54.       }  
  55.       var contactData = new List()  
  56.       {  
  57.          new ContactViewModel() { GroupId=1,Name="Name11",Email="Name11@hotmail.com",ThumbNail="/WP7SterlingLearning;Component/ThumbNails/11.jpg"},  
  58.          new ContactViewModel() { GroupId=1,Name="Name12",Email="Name12@hotmail.com",ThumbNail="/WP7SterlingLearning;Component/ThumbNails/12.jpg"},  
  59.          //others omitted…  
  60.          new ContactViewModel() { GroupId=10,Name="Name105",Email="Name105@hotmail.com",ThumbNail="/WP7SterlingLearning;Component/ThumbNails/105.jpg"}  
  61.       };  
  62.       foreach (var item in contactData)  
  63.       {  
  64.          App.Database.Save(item);  
  65.       }  
  66.    }  
  67.    public event PropertyChangedEventHandler PropertyChanged;  
  68.    private void NotifyPropertyChanged(String propertyName)  
  69.    {  
  70.       PropertyChangedEventHandler handler = PropertyChanged;  
  71.       if (null != handler)  
  72.       {  
  73.          handler(thisnew PropertyChangedEventArgs(propertyName));  
  74.       }  
  75.    }  

很容易看出,上面代碼的關(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)鍵代碼

  1. //省略其他引用…  
  2. using Wintellect.Sterling;  
  3. using WP7SterlingLearning.Database;  
  4. namespace WP7SterlingLearning  
  5. {  
  6.    public partial class App : Application  
  7.    {  
  8.       private static MainViewModel viewModel = null;  
  9.       private static ISterlingDatabaseInstance _database = null;  
  10.       private static SterlingEngine _engine = null;  
  11.       private static SterlingDefaultLogger _logger = null;  
  12.       //The MainViewModel object  
  13.       public static MainViewModel ViewModel  
  14.       {  
  15.          get 
  16.          {     
  17.             if (viewModel == null)  
  18.                viewModel = new MainViewModel();  
  19.             return viewModel;  
  20.          }  
  21.       }  
  22.       public static ISterlingDatabaseInstance Database  
  23.       {  
  24.          get 
  25.          {  
  26.              return _database;  
  27.          }  
  28.       }  
  29.       //… 

注意,上面的全局靜態(tài)屬性Database的定義有助于在應用程序的各處引用這一數(shù)據(jù)庫。此外,另一個全局靜態(tài)屬性ViewModel也起著類似的作用。

其次,我們定義了兩個輔助方法。***個方法_ActivateEngine旨在當程序***啟動時或者當程序從墓碑(tombstone)事件中喚醒時激活數(shù)據(jù)庫ContactsDatabase。第二個方法_DeactivateEngine用于當應用程序退出或進入到墓碑(tombstone)事件中時停用Sterling引擎。

列表3:方法_ActivateEngine的關(guān)鍵代碼部分

  1. private void _ActivateEngine()  
  2. {  
  3.    _engine = new SterlingEngine();  
  4.    _logger = new SterlingDefaultLogger(SterlingLogLevel.Information);  
  5.    _engine.Activate();  
  6.    _database = _engine.SterlingDatabase.RegisterDatabase();  
  7.    //register triggers  
  8.    var maxIdx1 =  
  9.    _database.Query().Any() ?  
  10.    (from id in _database.Query()  
  11.    select id.Key).Max() + 1 : 1;  
  12.    _database.RegisterTrigger(new ContactsDatabase.GroupTrigger(maxIdx1));  
  13.    var maxIdx2 =  
  14.    _database.Query().Any() ?  
  15.    (from id in _database.Query()  
  16.    select id.Key).Max() + 1 : 1;  
  17.    _database.RegisterTrigger(new ContactsDatabase.ContactTrigger(maxIdx2));  
  18. }  
  19.  
  20. private void _DeactivateEngine()  
  21. {  
  22.    _logger.Detach();  
  23.    _engine.Dispose();  
  24.    _database = null;  
  25.    _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引擎

  1. private void Application_Launching(object sender, LaunchingEventArgs e)  
  2. {  
  3.    _ActivateEngine();  
  4. }  
  5. private void Application_Activated(object sender, ActivatedEventArgs e)  
  6. {  
  7.    _ActivateEngine();  
  8.    if (!App.ViewModel.IsDataLoaded)  
  9.    {  
  10.       App.ViewModel.LoadData();  
  11.    }  
  12. }  
  13. private void Application_Deactivated(object sender, DeactivatedEventArgs e)  
  14. {  
  15.    _DeactivateEngine();  
  16. }  
  17. private void Application_Closing(object sender, ClosingEventArgs e)  
  18. {  
  19.    _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)鍵標記代碼

  1. <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Groups}"   
  2. SelectionChanged="MainListBox_SelectionChanged"> 
  3.     <ListBox.ItemTemplate> 
  4.         <DataTemplate> 
  5.             <StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal"> 
  6.                 <TextBlock Text="{Binding GroupId}" TextWrapping="Wrap"   
  7. Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
  8.                 <TextBlock Text="{Binding GroupName}" TextWrapping="Wrap" Margin="12,-  
  9. 6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
  10.             </StackPanel> 
  11.         </DataTemplate> 
  12.     </ListBox.ItemTemplate> 
  13. </ListBox> 

現(xiàn)在,稍有Siverlight經(jīng)驗的讀者應當都會理解上面的標記代碼。這里通過使用基本的數(shù)據(jù)綁定技術(shù)(我們使用ListBox)來渲染GroupViewModel實例的每一個屬性值。

接下來,讓我們看看主視圖頁面后臺代碼編寫的情況。

列表6:主視圖頁面后臺關(guān)鍵代碼

  1. namespace WP7SterlingLearning  
  2. {  
  3.    public partial class MainPage : PhoneApplicationPage  
  4.    {  
  5.       public MainPage()  
  6.       {  
  7.          InitializeComponent();  
  8.          DataContext = App.ViewModel;  
  9.          this.Loaded += new RoutedEventHandler(MainPage_Loaded);  
  10.       }  
  11.       private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  12.       {  
  13.          if (MainListBox.SelectedIndex == -1)  
  14.             return;  
  15.          NavigationService.Navigate(new Uri("/ContactList.xaml?selectedGroup=" + MainListBox.SelectedIndex, UriKind.Relative));  
  16.          MainListBox.SelectedIndex = -1;  
  17.       }  
  18.       private void MainPage_Loaded(object sender, RoutedEventArgs e)  
  19.       {  
  20.          if (!App.ViewModel.IsDataLoaded)  
  21.          {  
  22.             App.ViewModel.LoadData();  
  23.          }  
  24.       }  
  25.    }  

首先,在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標記代碼

  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  2.     <ListBox x:Name="MainListBox" Margin="0,0,-12,0" > 
  3.         <ListBox.ItemTemplate> 
  4.             <DataTemplate> 
  5.                 <StackPanel Margin="0,0,0,17" Width="432"> 
  6.                     <TextBlock Text="{Binding GroupId}" TextWrapping="Wrap" 
  7.  
  8. Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
  9.                     <TextBlock Text="{Binding Id}" TextWrapping="Wrap" Margin="12,-6,12,0" 
  10.  
  11. Style="{StaticResource PhoneTextSubtleStyle}"/> 
  12.                     <TextBlock Text="{Binding Name}" TextWrapping="Wrap" 
  13.  
  14. Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
  15.                     <TextBlock Text="{Binding Email}" TextWrapping="Wrap" Margin="12,-  
  16.  
  17. 6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/> 
  18. <Image Source="{Binding ThumbNail,Converter={StaticResource  
  19.  
  20. ImageConverter1}}" /> 
  21.                 </StackPanel> 
  22.             </DataTemplate> 
  23.         </ListBox.ItemTemplate> 
  24.     </ListBox> 
  25. </Grid> 

為了簡單起見,我們還利用一個ListBox控件來展示聯(lián)系人信息。同時,我們也使用了典型的Silverlight數(shù)據(jù)綁定技術(shù)。總體來看,上面代碼中只有一點值得注意,即圖像數(shù)據(jù)的顯示問題。為此,代碼中使用了一個自定義的類型轉(zhuǎn)換器。

列表8: 自定義的類型轉(zhuǎn)換器代碼

  1. public class imageUrlConverter : IValueConverter  
  2. {  
  3.    public object Convert(object value, Type targetType, object parameter,  
  4.    System.Globalization.CultureInfo culture)  
  5.    {  
  6.       if (value == null || parameter == null)  
  7.       {  
  8.          return value;  
  9.       }  
  10.       return new BitmapImage(new Uri((string)value, UriKind.RelativeOrAbsolute));  
  11.    }  
  12.    public object ConvertBack(object value, Type targetType, object parameter,  
  13.    System.Globalization.CultureInfo culture)  
  14.    {  
  15.       throw new NotImplementedException();  
  16.    }  

注意,在本示例實現(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)

  1. protected override void OnNavigatedTo(NavigationEventArgs e)  
  2. {  
  3.    string selectedGroup = "";  
  4.    if (NavigationContext.QueryString.TryGetValue("selectedGroup"out selectedGroup))  
  5.    {  
  6.       int iGroup = int.Parse(selectedGroup);  
  7.       // Query from Database by key  
  8.       var contactList =from key in 
  9.       App.Database.Query().Where(  
  10.          x => x.LazyValue.Value.GroupId == iGroup + 1).ToList() select key.LazyValue.Value;  
  11.       MainListBox.ItemsSource=contactList;  
  12.    }  

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的一點皮毛,還有更多更好的東西等著您自己去深入挖掘。

【編輯推薦】

  1. 為您介紹幾款開源的數(shù)據(jù)挖掘工具
  2. 告訴你如何解決MySQL server has gone away問題
  3. 數(shù)據(jù)庫中分組字符串相加
  4. SQL點滴之收集SQL Server線程等待信息
  5. 數(shù)據(jù)庫的性能已成重多廠商關(guān)注的焦點


 

責任編輯:艾婧 來源: it168
相關(guān)推薦

2011-05-18 10:21:53

SQLite

2011-05-18 09:30:16

SQLite

2011-02-28 10:42:14

Windows Pho微軟

2011-03-29 13:03:59

IronRubyWindows Pho.NET

2011-05-12 13:03:36

WP7數(shù)據(jù)庫選擇

2011-04-27 09:58:56

Windows PhoLBS微軟

2011-05-10 08:53:46

iOSWindows Pho開發(fā)者

2011-06-10 09:03:36

AndroidWindows Pho開發(fā)者

2011-08-19 09:09:01

AndroidWP7Windows Pho

2012-03-04 20:55:33

WP7

2012-07-06 09:26:13

Windows PhoWindows Pho

2010-09-03 08:57:26

本地數(shù)據(jù)庫

2012-01-01 19:33:19

2011-06-15 10:18:12

Windows PhoPerst

2011-09-22 14:20:10

雷軍小米WP7

2013-06-17 14:10:08

WP7開發(fā)Windows Pho豆瓣電臺

2013-06-17 13:47:41

WP7開發(fā)Windows Pho文本框水印控件

2011-03-08 10:26:45

Windows Pho諾基亞Qt

2011-07-28 09:26:18

MangoWindows Pho富士通

2012-06-21 09:07:22

微軟WP7WP8
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 久久不射电影网 | 99精品国产一区二区三区 | 中文字幕精品一区二区三区精品 | 久久久久久久国产精品视频 | 99精品99 | 久久国产高清视频 | 亚洲欧美日本在线 | 五月激情综合网 | 精品区 | 成人在线免费 | 久久99国产精品 | 91影视| 欧美日韩综合视频 | 中文字幕一区二区三区在线乱码 | 精品一区二区三区视频在线观看 | 亚洲精品视频在线观看免费 | 99免费在线视频 | 国产综合视频 | av在线免费观看网址 | 久久最新 | 久久久久久久久精 | 日韩电影中文字幕 | 9久久婷婷国产综合精品性色 | 国产激情视频在线免费观看 | 91精品久久久久久久久久 | 日韩不卡一二区 | 一区二区三区视频播放 | 日韩一区二区三区在线视频 | av黄色片在线观看 | 久久久精品一区二区 | 99久久婷婷国产精品综合 | 亚洲成人免费电影 | 精品久久国产老人久久综合 | 天天夜夜人人 | 亚洲国产一区二区三区 | 欧美一区永久视频免费观看 | 麻豆久久久9性大片 | 91在线一区二区 | 亚洲视频在线免费观看 | 亚洲午夜在线 | 精品一区二区三区在线观看 |