漫谈windows phone 7 独立存储(二)
IsolatedStorageSettings可以说是保存数据到独立存储最简单的方式,如果是键值对的数据类型,并想要持久地保存在独立存储中,这时就可以使用IsolatedStorageSettings。
在上篇文章中,我们谈了IsolatedStorageFile,今天我们来看一下IsolatedStorageSettings的用法。
IsolatedStorageSettings可以说是保存数据到独立存储最简单的方式,如果是键值对的数据类型,并想要持久地保存在独立存储中,这时就可以使用IsolatedStorageSettings。
当退出应用时,应用的配置内容会自动序列化至独立存储中的一个文件内,当应用launched或者activated时,又会使用先前的数据来填充字典。
在本例中,我们将对IsolatedStorageSettings进行CRUD操作。
XAML:
Code<phone:PhoneApplicationPage x:Class="IsolatedStorageSettingsDemo.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitleGrid is the name of the application and page title--> <Grid x:Name="TitleGrid" Grid.Row="0"> <TextBlock Text="Isolated Storage Settings Demo" x:Name="textBlockListTitle" Style="{StaticResource PhoneTextTitle1Style}" FontSize="30" /> </Grid> <!--ContentGrid is empty. Place new content here--> <Grid x:Name="ContentGrid" Grid.Row="1"> <TextBox Height="72" HorizontalAlignment="Left" Margin="172,46,0,0" Name="txtKey" Text="" VerticalAlignment="Top" Width="212" /> <Button Content="Save" Height="70" HorizontalAlignment="Left" Margin="78,228,0,0" Name="btnSave" VerticalAlignment="Top" Width="160" Click="btnSave_Click" /> <ListBox Height="168" HorizontalAlignment="Left" Margin="94,392,0,0" Name="lstKeys" VerticalAlignment="Top" Width="274" BorderThickness="1" SelectionChanged="lstKeys_SelectionChanged" /> <TextBlock Height="39" HorizontalAlignment="Left" Margin="94,62,0,0" Name="textBlock1" Text="Key" VerticalAlignment="Top" /> <TextBox Height="74" HorizontalAlignment="Left" Margin="172,124,0,0" Name="txtValue" Text="" VerticalAlignment="Top" Width="212" /> <TextBlock Height="39" HorizontalAlignment="Left" Margin="94,140,0,0" Name="textBlock2" Text="Value" VerticalAlignment="Top" /> <Button Content="Delete" Height="70" HorizontalAlignment="Left" Margin="224,228,0,0" Name="btnDelete" VerticalAlignment="Top" Width="160" Click="btnDelete_Click" /> <TextBlock Height="39" HorizontalAlignment="Left" Margin="94,347,0,0" Name="textBlock3" Text="List of Keys" VerticalAlignment="Top" /> </Grid> </Grid> </phone:PhoneApplicationPage>
CS:
Codeusing System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using System.IO.IsolatedStorage; namespace IsolatedStorageSettingsDemo { public partial class MainPage : PhoneApplicationPage { private IsolatedStorageSettings _appSetting; // Constructor public MainPage() { InitializeComponent(); SupportedOrientations = SupportedPageOrientation.Portrait; _appSetting = IsolatedStorageSettings.ApplicationSettings; } private void btnSave_Click(object sender, RoutedEventArgs e) { if (!string.IsNullOrEmpty(txtKey.Text)) { if (_appSetting.Contains(txtKey.Text)) { _appSetting[txtKey.Text] = txtValue.Text; } else { _appSetting.Add(txtKey.Text, txtValue.Text); } _appSetting.Save(); BindKeyList(); } } private void btnDelete_Click(object sender, RoutedEventArgs e) { if (lstKeys.SelectedIndex > -1) { _appSetting.Remove(lstKeys.SelectedItem.ToString()); _appSetting.Save(); BindKeyList(); } } private void BindKeyList() { lstKeys.Items.Clear(); foreach (string key in _appSetting.Keys) { lstKeys.Items.Add(key); } txtKey.Text = ""; txtValue.Text = ""; } private void lstKeys_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count > 0) { string key = e.AddedItems[0].ToString(); if (_appSetting.Contains(key)) { txtKey.Text = key; txtValue.Text = _appSetting[key].ToString(); } } } } }
效果图: