chenqb 发表于 2015-5-9 08:16:20

Windows Phone 7 不温不火学习之《独立存储空间》

  在Android 里面我们要快速保存用户的设置或者游戏进行的数据,我们通常全使用SharePreference 这个类来进行操作,另外Android 还提供了一系列继承自SharePreference 的组件提供我们快速保存用户的设置项。那么在Windows Phone 7 提供了什么样的机制提供用户快速保存数据呢?微软使用了一个叫IsolatedStorageSettings 的类库提供给开发人员快速的使用独立存储保存用户数据的功能,但总体使用感觉来说没有Android 使用的方便,另外Andriod 的类似这种数据存储是暴露给用户的,而Windows Phone 7 的这种存储机制则是严格控制,我们开发人员也不知道其具体存放位置。本篇的学习笔记,我利用Android 的存储特色使用Windows Phone 7 的 IsolatedStorageSettings 类模仿了一个类似 Android存储机制的DEMO,希望这个小DEMO能对你有所帮助,下面先给出效果图:

  首次进入页面时,界面放置了一个 TextBlock 控件和一个导航的按钮,点击导航按钮进入配置界面,如下图:

  如上图,图中有一系列组件,可以设置第一幅图的TextBlock 的字体大小和字体需要显示的颜色,选择红色并且改变字体大小为32,然后点击Back 键退回来,显示效果如下:

  虽说这是一个非常简单的DEMO,但Windows Phone 7的IsolatedStorageSettings 在这个DEMO中的使用是比较全面的,下面给出代码示意:
  首先,mainPage 界面的CS代码
  


public partial class MainPage : PhoneApplicationPage
    {
      // Constructor
      public MainPage()
      {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
      }
      void MainPage_Loaded(object sender, RoutedEventArgs e)
      {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(App.textColor))
            {
                int colorInt = Int32.Parse(IsolatedStorageSettings.ApplicationSettings.ToString());
                SolidColorBrush scb=null;
                switch (colorInt)
                {
                  case 0:
                         scb = new SolidColorBrush(Colors.White);
                        break;
                  case 1:
                         scb = new SolidColorBrush(Colors.Red);
                        break;
                  case 2:
                        scb = new SolidColorBrush(Colors.Green);
                        break;
                  case 3:
                        scb = new SolidColorBrush(Colors.Blue);
                        break;
                }
                setTextBlock.Foreground = scb;
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(App.textSize))
            {
                int textSize = Int32.Parse(IsolatedStorageSettings.ApplicationSettings.ToString());
                setTextBlock.FontSize = textSize;
            }
      }
      private void navigateionBtn_Click(object sender, RoutedEventArgs e)
      {
            NavigationService.Navigate(new Uri("/Set.xaml",UriKind.RelativeOrAbsolute));
      }  
  当界面加载时,读取配置信息并修改TextBlock 对应的值。进入配置页面时稍显复杂,考虑到不需要用户点确认就可以保存数据,这里使用的导航进来和导航出去两种写法,代码如下:
  


public partial class Set : PhoneApplicationPage
    {
      int textColor;
      int textSize;
      public Set()
      {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Set_Loaded);
      }
      void Set_Loaded(object sender, RoutedEventArgs e)
      {
            switch (textColor)
            {
                case 0:
                  whiteRadioButton.IsChecked = true;
                  break;
                case 1:
                  redRadioButton.IsChecked = true;
                  break;
                case 2:
                  greenRadioButton.IsChecked = true;
                  break;
                case 3:
                  blueRadioButton.IsChecked = true;
                  break;
            }
            textSizeTextBox.Text = textSize.ToString();
      }
      
      private int colorInt = -1;
      protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
      {
            if (whiteRadioButton.IsChecked==true)
            {
                colorInt = 0;
            }
            else if (redRadioButton.IsChecked==true)
            {
                colorInt = 1;
            }
            else if (greenRadioButton.IsChecked==true)
            {
                colorInt = 2;
            }
            else if (blueRadioButton.IsChecked==true)
            {
                colorInt = 3;
            }
            IsolatedStorageSettings.ApplicationSettings = colorInt;
            IsolatedStorageSettings.ApplicationSettings = Int32.Parse(textSizeTextBox.Text);
            IsolatedStorageSettings.ApplicationSettings.Save();
            base.OnNavigatedFrom(e);
      }
      protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
      {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(App.textColor))
            {
               textColor = Int32.Parse(IsolatedStorageSettings.ApplicationSettings.ToString());
               
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(App.textSize))
            {
               textSize = Int32.Parse(IsolatedStorageSettings.ApplicationSettings.ToString());
            
            }
            base.OnNavigatedTo(e);
      }
    }  
  导航进来时,为变量赋值,并在加载完成后改变控件的内容和状态,如上图的TextBox 的文本值和RadioButton的选中项。
  导航出去时,保存用户的选择行为。
  Tip:IsolatedStorageSettings.ApplicationSettings 这个 Ditonary 的Value 是一个 Object ,亦就是您可以将你的一个Model 对象保存进来亦可,它内部会帮你序列化。
  另外,如果你习惯使用IO流写文件操作,下篇文章会讲述到使用IO流写文件的方式存储空间的方法,希望 留意。
  源码下载:存储空间DEMO
页: [1]
查看完整版本: Windows Phone 7 不温不火学习之《独立存储空间》