1
我正在嘗試創建和隔離存儲設置,用戶可以在其中打開或關閉聲音或震動警報(或同時打開或關閉),具體取決於用戶設置頁面中的切換開關。我正在使用mvvm框架,需要引用MainViewModel類中的Setting類。我意識到我需要插入if語句作爲過程的一部分,但遇到困難。Windows Phone 7.1 - 使用ToggleSwitch打開和關閉聲音的獨立存儲設置
Settings.xaml
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<toolbox:ToggleSwitch Name="MobileSound" Header="Sound"></toolbox:ToggleSwitch>
<toolbox:ToggleSwitch Name="MobileVibrate" Header="Vibrate" Margin="6,119,6,371"></toolbox:ToggleSwitch>
<Slider Height="84" HorizontalAlignment="Left" Margin="12,310,0,0" Name="slider1" VerticalAlignment="Top" Width="418" Value="1" Minimum="1" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,274,0,0" Name="Difficulty" Text="Refresh Timer" VerticalAlignment="Top" Width="135" />
</Grid>
Settings.cs
using System;
using System.Collections.Generic;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Windows.Navigation;
namespace MobileApplicationSample
{
/// <summary>
/// Description for Setting.
/// </summary>
public partial class Setting : PhoneApplicationPage
{
// Constructor
public Setting()
{
InitializeComponent();
settings = IsolatedStorageSettings.ApplicationSettings;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
System.Diagnostics.Debug.WriteLine("into the app");
try
{
System.Diagnostics.Debug.WriteLine("Retrieving values");
MobileSound.IsChecked = (bool)settings["sound"];
MobileVibrate.IsChecked = (bool)settings["vibrate"];
slider1.Value = (Int16)settings["diff"];
}
catch (KeyNotFoundException)
{
System.Diagnostics.Debug.WriteLine("First Time using the app");
settings.Add("sound", false);
settings.Add("vibrate", false);
settings.Add("diff", 1);
settings.Save();
}
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Exiting, so save now");
settings["vibrate"] = MobileVibrate.IsChecked;
settings["sound"] = MobileSound.IsChecked;
settings["diff"] = (Int16)slider1.Value;
settings.Save();
}
public IsolatedStorageSettings settings { get; set; }
}
}