How to : Create a Setting Page For Windows Phone
using System;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;
namespace SettingsSample
{
public class AppSettings
{
// Our isolated storage settings
IsolatedStorageSettings isolatedStore;
// The isolated storage key names of our settings
const string CheckBoxSettingKeyName = "CheckBoxSetting";
const string ListBoxSettingKeyName = "ListBoxSetting";
const string RadioButton1SettingKeyName = "RadioButton1Setting";
const string RadioButton2SettingKeyName = "RadioButton2Setting";
const string RadioButton3SettingKeyName = "RadioButton3Setting";
const string UsernameSettingKeyName = "UsernameSetting";
const string PasswordSettingKeyName = "PasswordSetting";
// The default value of our settings
const bool CheckBoxSettingDefault = true;
const int ListBoxSettingDefault = 0;
const bool RadioButton1SettingDefault = true;
const bool RadioButton2SettingDefault = false;
const bool RadioButton3SettingDefault = false;
const string UsernameSettingDefault = "";
const string PasswordSettingDefault = "";
///
/// Constructor that gets the application settings.
///
public AppSettings()
{
try
{
// Get the settings for this application.
isolatedStore = IsolatedStorageSettings.ApplicationSettings;
}
catch (Exception e)
{
Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
}
}
///
/// Update a setting value for our application. If the setting does not
/// exist, then add the setting.
///
///
///
///
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
// If the key exists
if (isolatedStore.Contains(Key))
{
// If the value has changed
if (isolatedStore[Key] != value)
{
// Store the new value
isolatedStore[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
isolatedStore.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}
///
/// Get the current value of the setting, or if it is not found, set the
/// setting to the default setting.
///
///
///
///
///
public valueType GetValueOrDefault(string Key, valueType defaultValue)
{
valueType value;
// If the key exists, retrieve the value.
if (isolatedStore.Contains(Key))
{
value = (valueType)isolatedStore[Key];
}
// Otherwise, use the default value.
else
{
value = defaultValue;
}
return value;
}
///
/// Save the settings.
///
public void Save()
{
isolatedStore.Save();
}
///
/// Property to get and set a CheckBox Setting Key.
///
public bool CheckBoxSetting
{
get
{
return GetValueOrDefault(CheckBoxSettingKeyName, CheckBoxSettingDefault);
}
set
{
AddOrUpdateValue(CheckBoxSettingKeyName, value);
Save();
}
}
///
/// Property to get and set a ListBox Setting Key.
///
public int ListBoxSetting
{
get
{
return GetValueOrDefault(ListBoxSettingKeyName, ListBoxSettingDefault);
}
set
{
AddOrUpdateValue(ListBoxSettingKeyName, value);
Save();
}
}
///
/// Property to get and set a RadioButton Setting Key.
///
public bool RadioButton1Setting
{
get
{
return GetValueOrDefault(RadioButton1SettingKeyName, RadioButton1SettingDefault);
}
set
{
AddOrUpdateValue(RadioButton1SettingKeyName, value);
Save();
}
}
///
/// Property to get and set a RadioButton Setting Key.
///
public bool RadioButton2Setting
{
get
{
return GetValueOrDefault(RadioButton2SettingKeyName, RadioButton2SettingDefault);
}
set
{
AddOrUpdateValue(RadioButton2SettingKeyName, value);
Save();
}
}
///
/// Property to get and set a RadioButton Setting Key.
///
public bool RadioButton3Setting
{
get
{
return GetValueOrDefault(RadioButton3SettingKeyName, RadioButton3SettingDefault);
}
set
{
AddOrUpdateValue(RadioButton3SettingKeyName, value);
Save();
}
}
///
/// Property to get and set a Username Setting Key.
///
public string UsernameSetting
{
get
{
return GetValueOrDefault(UsernameSettingKeyName, UsernameSettingDefault);
}
set
{
AddOrUpdateValue(UsernameSettingKeyName, value);
Save();
}
}
///
/// Property to get and set a Password Setting Key.
///
public string PasswordSetting
{
get
{
return GetValueOrDefault(PasswordSettingKeyName, PasswordSettingDefault);
}
set
{
AddOrUpdateValue(PasswordSettingKeyName, value);
Save();
}
}
}
}
x:Class="SettingsSample.SettingsWithoutConfirmation"
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"
xmlns:local="clr-namespace:SettingsSample"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">
posted on 2011-12-03 15:58 thankchunzi 阅读(139) 评论(0) 编辑 收藏 举报