我正在尝试创建和隔离存储设置,用户可以根据用户设置页面中切换开关的状态打开或关闭声音或振动警报(或两者一起)。我正在使用mvvm框架,需要在MainViewModel类中引用Setting类。我意识到我需要插入一个if语句作为进程的一部分,但是遇到了困难。
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; }
}
}
答案 0 :(得分:0)
首先定义一个切换开关...... 我正在使用一个切换开关,您可以根据2进行更改。
<toolkit:ToggleSwitch Foreground="Black" Background="Black" FontWeight="Medium" IsChecked="{Binding LocationSetting, Mode=TwoWay, Source={StaticResource appSettings}}" Margin="0,15,0,0" />
然后在你的设置类中。
public class AppSettings : ViewModelBase
{
IsolatedStorageSettings settings;
const string LocationSettingName = "sound";
const bool LocationSettingDefault = false;
public AppSettings()
{
// Get the settings for this application.
settings = IsolatedStorageSettings.ApplicationSettings;
}
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
// If the key exists
if (settings.Contains(Key))
{
// If the value has changed
if (settings[Key] != value)
{
// Store the new value
settings[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
settings.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}
public T GetValueOrDefault<T>(string Key, T defaultValue)
{
T value;
// If the key exists, retrieve the value.
if (settings.Contains(Key))
{
value = (T)settings[Key];
}
// Otherwise, use the default value.
else
{
value = defaultValue;
}
return value;
}
public void Save()
{
settings.Save();
}
public bool LocationSetting
{
get
{
return GetValueOrDefault<bool>(LocationSettingName, LocationSettingDefault);
}
set
{
if (AddOrUpdateValue(LocationSettingName, value))
{
Save();
}
}
}
}