利用IsolatedStorageSettings类可以很方便的保存用户的设置,非常类似安卓的Sharepreference。
下面这个例子是设置APP背景色,APP利用IsolatedStorageSettings来记忆用户的选择。
下面是XAML代码:
<phone:PhoneApplicationPage
x:Class="PhoneApp3.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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<Grid Name="root" HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="480">
<Button Content="SAVE" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="167,471,0,0" Click="Button_Click"/>
<RadioButton x:Name="RedBt" GroupName="Rb" Content="Red" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="167,212,0,0" Checked="RedBt_Checked"/>
<RadioButton x:Name="YellowBt" GroupName="Rb" Content="Yellow" HorizontalAlignment="Left" Margin="167,289,0,0" VerticalAlignment="Top" Checked="YellowBt_Checked"/>
<RadioButton x:Name="BlackBt" GroupName="Rb" Content="Black" HorizontalAlignment="Left" Margin="167,361,0,0" VerticalAlignment="Top" Checked="BlackBt_Checked"/>
</Grid>
<!--LayoutRoot 是包含所有页面内容的根网格-->
</phone:PhoneApplicationPage>
下面是CS代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp3.Resources;
using System.Windows.Media;
using System.IO.IsolatedStorage;
namespace PhoneApp3
{
public partial class MainPage : PhoneApplicationPage
{
private static int MyColor;
private IsolatedStorageSettings MyStore;
private SolidColorBrush MyPaint = new SolidColorBrush();
// 构造函数
public MainPage()
{
InitializeComponent();
MyStore = IsolatedStorageSettings.ApplicationSettings;
root.Background = MyPaint;
if (MyStore.Contains("MyColor"))
{
if ((int)MyStore["MyColor"] == 1)
{
MyColor = 1;
MyPaint.Color = Colors.Red;
}
if ((int)MyStore["MyColor"] == 2)
{
MyColor = 2;
MyPaint.Color = Colors.Yellow;
}
if ((int)MyStore["MyColor"] == 3)
{
MyColor = 3;
MyPaint.Color = Colors.Black;
}
}else
{
MyStore.Add("MyColor", (object)3);
}
}
private void RedBt_Checked(object sender, RoutedEventArgs e)
{
MyColor = 1;
MyPaint.Color = Colors.Red;
}
private void YellowBt_Checked(object sender, RoutedEventArgs e)
{
MyColor = 2;
MyPaint.Color = Colors.Yellow;
}
private void BlackBt_Checked(object sender, RoutedEventArgs e)
{
MyColor = 3;
MyPaint.Color = Colors.Black;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MyStore["MyColor"] = MyColor;
MyStore.Save();
}
}
}