2014-06-07 20 views
0

我在Windows Phone 8上添加了一個切換按鈕。當我檢查(上)時,它將值保存在獨立存儲中,並在構造函數中檢查該值,無論它具有切換值還是爲空值。如果有切換值,我想顯示切換按鈕。但是我不知道應用程序運行時如何檢查它的屬性。切換按鈕選中屬性Windows Phone 8

切換按鈕XAML:

<toolkit:ToggleSwitch x:Name="toggle" Content="On" Width="165" FontSize="28" VerticalAlignment="Center" HorizontalAlignment="Right"/> 

C#:

public Subscription() 
    { 
     InitializeComponent(); 
     this.toggle.Checked += new EventHandler<RoutedEventArgs>(toggle_Checked); 
     this.toggle.Unchecked += new EventHandler<RoutedEventArgs>(toggle_Unchecked); 

     var appSettings = IsolatedStorageSettings.ApplicationSettings; 


     if (HasSValue() == "NoValue") 
     { 
      // Here i want to Display toggle button unchecked 
     } 
     else 
     { 
      // Here i want to Display toggle button checked 

     } 
    } 

    void toggle_Unchecked(object sender, RoutedEventArgs e) 
    { 
     this.toggle.Content = "Off"; 
     this.toggle.SwitchForeground = new SolidColorBrush(Colors.Red); 

     var appSettings = IsolatedStorageSettings.ApplicationSettings; 
     appSettings.Remove("toggleValue"); 
     appSettings.Save(); 

    } 

    void toggle_Checked(object sender, RoutedEventArgs e) 
    { 
     this.toggle.Content = "On"; 
     this.toggle.SwitchForeground = new SolidColorBrush(Colors.Green); 
     MessageBox.Show("R U Sure ?"); 
     var appSettings = IsolatedStorageSettings.ApplicationSettings; 
     appSettings.Add("toggleValue", "MAHIN"); 
     appSettings.Save(); 
    } 

    public string HasSValue() 
    { 
     var appSettings = IsolatedStorageSettings.ApplicationSettings; 
     if (appSettings.Contains("toggleValue")) 
     { 
      return (string) appSettings["toggleValue"]; 
     } 
     else 
     { 
      return "NoValue"; 
     } 
    } 

回答

0

試試這個

if (HasSValue() == "NoValue") 
    { 
     this.toggle.IsChecked = false; 
    } 
    else 
    { 
     this.toggle.IsChecked = true; 

    } 

希望這有助於

更新您的構造函數如下

public Subscription() 
{ 
    InitializeComponent(); 

    var appSettings = IsolatedStorageSettings.ApplicationSettings; 


    if (HasSValue() == "NoValue") 
    { 
     // Here i want to Display toggle button unchecked 
    } 
    else 
    { 
     // Here i want to Display toggle button checked 

    } 

    this.toggle.Checked += new EventHandler<RoutedEventArgs>(toggle_Checked); 
    this.toggle.Unchecked += new EventHandler<RoutedEventArgs>(toggle_Unchecked); 
} 
+0

它的工作,但問題是,當我檢查這個切換重新訪問此網頁形成另一個頁面時,toggle_Checked()是調用再怎麼我檢查切換狀態的構造。那我該如何擺脫它呢? –

+0

根據我對這個答案的編輯更新你的構造函數 –

+0

Thnx bro,它的工作正常。你可以解釋這兩條線 this.toggle.Checked + = new EventHandler (toggle_Checked); this.toggle.Unchecked + = new EventHandler (toggle_Unchecked); –