1.IsolatedStorageSettings 提供一个在独立存储中存储键/值对的 Dictionary<(Of <(<'TKey, TValue>)>)>。
前台代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<UserControl x:Class="IsolatedStorageSample.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FontFamily="Trebuchet MS" FontSize="11"
Width="500" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel VerticalAlignment="Top" >
<TextBlock x:Name="tbGreeting" FontSize="24" />
<StackPanel Height="30" Orientation="Horizontal" Margin="10">
<TextBlock Text="Name: " VerticalAlignment="Center"/>
<TextBox x:Name="tbName" Width="230" Margin="0,0,5,0"/>
<Button x:Name="btnAddName" Content="Add" Click="btnAddName_Click" />
<Button x:Name="btnChangeName" Content="Change" Click="btnChangeName_Click" />
<Button x:Name="btnRemoveName" Content="Remove" Click="btnRemoveName_Click" />
</StackPanel>
<StackPanel Height="30" Orientation="Horizontal" Margin="10">
<TextBlock Text="Settings: " VerticalAlignment="Center"/>
<Button x:Name="btnClear" Content="Clear" Click="btnClear_Click" />
<Button x:Name="btnCount" Content="Count" Click="btnCount_Click" />
<Button x:Name="btnKeys" Content="Get Keys" Click="btnKeys_Click" />
<Button x:Name="btnValues" Content="Get Values" Click="btnValues_Click" />
</StackPanel>
<StackPanel Height="30" Orientation="Horizontal" Margin="10">
<TextBlock Text="Results: " VerticalAlignment="Center"/>
<TextBox x:Name="tbResults" Width="230" Margin="0,0,5,0"/>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.IO.IsolatedStorage;
namespace IsolatedStorageSample
{
public partial class Page : UserControl
{
private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;
public Page()
{
InitializeComponent();
// Retrieve and set user name.
try
{
string name = (string)userSettings["name"];
tbGreeting.Text = "Hello, " + name;
}
catch (System.Collections.Generic.KeyNotFoundException)
{
// No preference is saved.
tbGreeting.Text = "Hello, World";
}
}
private void btnAddName_Click(object sender, RoutedEventArgs e)
{
try
{
userSettings.Add("name", tbName.Text);
tbResults.Text = "Name saved. Refresh page to see changes.";
}
catch (ArgumentException ex)
{
tbResults.Text = ex.Message;
}
}
private void btnChangeName_Click(object sender, RoutedEventArgs e)
{
userSettings["name"] = tbName.Text;
tbResults.Text = "Name changed. Refresh page to see changes.";
}
private void btnRemoveName_Click(object sender, RoutedEventArgs e)
{
if (userSettings.Remove("name") == true)
{
tbResults.Text = "Name removed. Refresh page to see changes.";
}
else
{
tbResults.Text = "Name could not be removed. Key does not exist.";
}
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
userSettings.Clear();
tbResults.Text = "Settings cleared. Refresh page to see changes.";
}
private void btnCount_Click(object sender, RoutedEventArgs e)
{
tbResults.Text = "Count: " + userSettings.Count();
}
private void btnKeys_Click(object sender, RoutedEventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder("Keys: ");
foreach (string k in userSettings.Keys)
{
sb.Append(k + "; ");
}
tbResults.Text = sb.ToString();
}
private void btnValues_Click(object sender, RoutedEventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder("Values: ");
foreach (Object v in userSettings.Values)
{
sb.Append(v.ToString() + "; ");
}
tbResults.Text = sb.ToString();
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
if (!appSettings.Contains("UserName"))
{
appSettings.Add("UserName", string.Empty);
}
appSettings["UserName"] = UserName.Text;
StatusIndicator.Text = "Saved Username!";
if (appSettings.Contains("UserName"))
{
UserName.Text = appSettings["UserName"].ToString();
StatusIndicator.Text = "User name loaded from IsoStore.";
}