Winphone开发之保存设置

利用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();
        }

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值