【问题标题】:How to Keep Toggle Button state in Windows phone 8 application?如何在 Windows phone 8 应用程序中保持切换按钮状态?
【发布时间】:2014-07-23 11:17:56
【问题描述】:

我创建了一个切换按钮,它工作正常。但问题是我如何保持以前的切换活动,我的意思是当应用程序退出并重新打开时,它应该显示以前的切换状态。这是我的代码

XAML:

<toolkit:ToggleSwitch x:Name="toggle" Content="ToggleSwitch is on" Header="ToggleSwitch"/>

CS:

public partial class EnglishSub : PhoneApplicationPage
{
    public BanglaSub()
    {
        InitializeComponent();

        this.toggle.Checked += new EventHandler<RoutedEventArgs>(toggle_Checked);
        this.toggle.Unchecked += new EventHandler<RoutedEventArgs>(toggle_Unchecked);
        this.toggle.Content = "ToggleSwitch is off";
    }

    void toggle_Unchecked(object sender, RoutedEventArgs e)
    {
        this.toggle.Content = "ToggleSwitch is off";
        this.toggle.SwitchForeground = new SolidColorBrush(Colors.Red);
        MessageBox.Show("Disable");
    }

    void toggle_Checked(object sender, RoutedEventArgs e)
    {
        this.toggle.Content = "ToggleSwitch is on";
        this.toggle.SwitchForeground = new SolidColorBrush(Colors.Green);
        MessageBox.Show("Enable");
    }
}

【问题讨论】:

  • 我更新了你的代码来解决这个问题

标签: windows-phone-8 silverlight-toolkit togglebutton


【解决方案1】:

您可以使用 IsolatedStorageSetings 来存储应用程序数据。并在您的应用页面再次加载时阅读。这是怎么回事

public bool GetToggleValue()
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains("toggleValue"))
        {
           return  bool.Parse(IsolatedStorageSettings.ApplicationSettings["toggleValue"].ToString());


        }
        else return false;
    }

在页面加载中调用上述方法来设置切换值

并在您的已选中未选中事件处理程序的设置中设置已选中未选中值

IsolatedStorageSettings.ApplicationSettings.Add("toggleValue", true);
IsolatedStorageSettings.Save();

【讨论】:

  • 修改设置后不要忘记IsolatedStorageSettings.Save(),否则退出应用时该值不会被存储。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多