切换按钮已检查属性Windows Phone 8

时间:2014-06-07 09:42:51

标签: windows-phone-8 togglebutton

我在Windows Phone 8上添加了一个切换按钮。当我检查(打开)时,它在隔离存储中保存一个值,并在构造函数中检查该值,无论它具有切换值还是null。如果有切换值我想要显示切换按钮。但我不知道应用程序运行时如何检查它的属性。

切换按钮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";
        }
    }

1 个答案:

答案 0 :(得分: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);
}