C#接受按钮

时间:2015-07-07 11:49:04

标签: c# windows-phone-8.1

我正在为Windows Phone 8.1构建通用应用程序。

我有一个设置页面,其中只有一个拨动开关。 我试图找出如何保存切换开关状态。应用程序关闭后再次打开。

这是我尝试的方式:

private void Button_Click(object sender, RoutedEventArgs e)
{
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    if (!settings.Contains(TSwitch.IsOn))
    {
        settings.Add(TSwitch.IsOn);
    }
    settings.Save();
    this.Frame.Navigate(typeof(MainPage), null);
}

1 个答案:

答案 0 :(得分:0)

试试这个:

private void Button_Click(object sender, RoutedEventArgs e)
{
if (TSwitch.IsOn)
        {
            Windows.Storage.ApplicationData.Current.LocalSettings.Values["TSwitch"] = "Set";
            Windows.Storage.ApplicationData.Current.LocalSettings.Values["TSwitchValue"] = "on";
        }
        else
        {
        Windows.Storage.ApplicationData.Current.LocalSettings.Values["TSwitch"] = "NotSet";
        Windows.Storage.ApplicationData.Current.LocalSettings.Values["TSwitchValue"] = "off";
        }
}

然后在App.xaml.cs中添加:

        switch ((string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["TSwitchValue"])
        {
            case "Set":
                {
                 //Do something
                   break;
                }
           case "NotSet": 
                {
                //Do something
               break;
                }                
           default:
                {
                  //Do something 
                    break;
                }
        }

您可以根据切换开关要求更改案例选项。