WP8数据存储--独立存储设置

 <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>



        <!--TitlePanel 包含应用程序的名称和页标题-->

        <StackPanel Grid.Row="0" Margin="12,17,0,28">

            <TextBlock Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>

            <TextBlock Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

        </StackPanel>



        <!--ContentPanel - 在此处放置其他内容-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="24,10,0,-10">

            <TextBlock Margin="19,46,345,522" Style="{StaticResource PhoneTextTitle1Style}" FontSize="25" Text="Key"/>

            <TextBlock Margin="19,112,345,456" Style="{StaticResource PhoneTextTitle1Style}" FontSize="25" Text="Value"/>

            <TextBox x:Name="text_Key" HorizontalAlignment="Left" Height="58" Margin="116,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="260" FontSize="15"/>

            <TextBox x:Name="text_Value" HorizontalAlignment="Left" Height="58" Margin="116,101,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="260" FontSize="15"/>

            <Button x:Name="btn_Save" Content="保存" HorizontalAlignment="Left" Height="67" Margin="19,200,0,0" VerticalAlignment="Top" Width="122"/>

            <Button x:Name="btn_Delete" Content="删除" HorizontalAlignment="Left" Height="67" Margin="146,200,0,0" VerticalAlignment="Top" Width="122"/>

            <Button x:Name="deleteall" Content="清空所有" HorizontalAlignment="Left" Height="67" Margin="289,200,0,0" VerticalAlignment="Top" Width="144"/>

            <ListBox x:Name="lstKeys" Margin="31,267,35,69"/>

        </Grid>

    </Grid>
View Code
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 System.IO.IsolatedStorage;



namespace PhoneApp1

{

    public partial class Panorama : PhoneApplicationPage

    {

        public IsolatedStorageSettings _Settings;

        public Panorama()

        {

         

            InitializeComponent();

            _Settings = IsolatedStorageSettings.ApplicationSettings;

            BindkeyList();

            btn_Save.Click += btn_Save_Click;

            btn_Delete.Click += btn_Delete_Click;

            deleteall.Click += deleteall_Click;

            lstKeys.SelectionChanged += lstKeys_SelectionChanged;

        }



        void lstKeys_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            if (e.AddedItems.Count > 0)

            {

                string key = e.AddedItems[0].ToString();

                if (_Settings.Contains(key))

                {

                    text_Key.Text = key;

                    text_Value.Text = _Settings[key].ToString();

                }

            }

        }



        void deleteall_Click(object sender, RoutedEventArgs e)

        {

            _Settings.Clear();

            _Settings.Save();

            BindkeyList();

        }



        void btn_Delete_Click(object sender, RoutedEventArgs e)

        {

            if(lstKeys.SelectedIndex>-1)

            _Settings.Remove(lstKeys.SelectedItem.ToString());

            _Settings.Save();

            BindkeyList();



        }



        private void BindkeyList()

        {

            lstKeys.Items.Clear();

            if (_Settings!=null)

            {

                foreach (string key in _Settings.Keys)

                {

                    lstKeys.Items.Add(key);

                }

                text_Key.Text = "";

                text_Value.Text = "";

            }

        }



        void btn_Save_Click(object sender, RoutedEventArgs e)

        {

            if (text_Key.Text != "")

            {

                if (_Settings.Contains(text_Key.Text))

                {

                    _Settings[text_Key.Text] = text_Value.Text;

                }

                else

                {

                    _Settings.Add(text_Key.Text, text_Value.Text);

                }

                _Settings.Save();

                BindkeyList();

            }

            else

            { MessageBox.Show("请添加key值"); }

        }



       

    }

}
View Code

 

你可能感兴趣的:(数据存储)