2013-07-10 77 views
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; } 
} 

}

回答

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(); 
        } 
       } 
      } 
     }