xaml
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Height="39" HorizontalAlignment="Left" Margin="30,42,0,0" Name="textblock1" Text="Key" VerticalAlignment="Top"></TextBlock>
<TextBox Height="72" HorizontalAlignment="Left" Margin="115,24,0,0" Name="textKey" Text="" VerticalAlignment="Top" Width="212"></TextBox>
<TextBox Height="74" HorizontalAlignment="Left" Margin="115,109,0,0" Name="textValue" Text="" VerticalAlignment="Top" Width="212"></TextBox>
<TextBlock Height="39" HorizontalAlignment="Left" Margin="12,131,0,0" Name="textblock2" Text="Value" VerticalAlignment="Top" ></TextBlock>
<Button Content="儲存" Height="70" HorizontalAlignment="Left" Margin="0,255,0,0" Name="ButtonSave" VerticalAlignment="Top" Width="160" Click="ButtonSave_Click"></Button>
<Button Content="刪除" Height="70" HorizontalAlignment="Left" Margin="150,255,0,0" Name="ButtonDelete" VerticalAlignment="Top" Width="160" Click="ButtonDelete_Click"></Button>
<TextBlock Height="39" HorizontalAlignment="Left" Margin="94,347,0,0" Name="TextBlock3" Text="Keys列表" VerticalAlignment="Top" ></TextBlock>
<ListBox Height="168" HorizontalAlignment="Left" Margin="94,392,0,0" Name="ListBoxKeys" VerticalAlignment="Top" Width="274" BorderThickness="1" SelectionChanged="ListBoxKeys_SelectionChanged"></ListBox>
<Button Content="清空所有" Height="72" HorizontalAlignment="Left" Margin="308,255,0,0" Name="ButtonReset" VerticalAlignment="Top" Width="160" Click="ButtonReset_Click"></Button>
</Grid>
.cs
public partial class IsolatedStorageSettingPage : PhoneApplicationPage
{
private IsolatedStorageSettings _appSettings;
// 建構函式
public IsolatedStorageSettingPage()
{
InitializeComponent();
// 取得應用程式 IsolatedStorageSettings儲存
_appSettings = IsolatedStorageSettings.ApplicationSettings;
// 綁定到List控制項
BindKeyList();
}
// 儲存 IsoltedStorageSettings鍵值
private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
// 檢查輸入框不可為NULL
if(!String.IsNullOrEmpty(textKey.Text.Trim()))
{
// 如果key值已經存在則修改它的值
if(_appSettings.Contains(textKey.Text.Trim()))
_appSettings[textKey.Text.Trim()] = textValue.Text.Trim();
// 不存在則新增一個Key值
else _appSettings.Add(textKey.Text.Trim(), textValue.Text.Trim());
// 儲存獨立儲存設定
_appSettings.Save();
BindKeyList();
}
else MessageBox.Show("請輸入Key值");
}
// 刪除在List中選中的獨立儲存設定鍵
private void ButtonDelete_Click(object sender, RoutedEventArgs e)
{
// 如果選取了List中的某項
if (ListBoxKeys.SelectedIndex > -1)
{
// 移除這個鍵的獨立儲存設定
_appSettings.Remove(ListBoxKeys.SelectedItem.ToString());
_appSettings.Save();
BindKeyList();
}
}
// List控制項勾選項的事件 將勾選的鍵和值 顯示在上面的文字標籤中
private void ListBoxKeys_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
// 取得在List中選擇的Key
string key = e.AddedItems[0].ToString();
// 檢查獨立儲存設定是否存在這個KEY
if (_appSettings.Contains(key))
{
textKey.Text = key;
// 取得Key得值並且顯示在文字標籤上
textValue.Text = _appSettings[key].ToString();
}
}
}
// 將目前程式中所有的Key值綁訂到List上
private void BindKeyList()
{
// 先清空List控制項的綁定值
ListBoxKeys.Items.Clear();
// 取得目前應用程式的所有KEY 增加到List控制項
foreach (string key in _appSettings.Keys)
ListBoxKeys.Items.Add(key);
textKey.Text = string.Empty;
textValue.Text = string.Empty;
}
private void ButtonReset_Click(object sender, RoutedEventArgs e)
{
// 清空所有KEY
_appSettings.Clear();
BindKeyList();
}
}