从哪里访问隔离存储值?

时间:2012-06-06 05:50:26

标签: windows-phone-7 isolatedstorage

  public IsolatedStorageSettings appSettings =
                  IsolatedStorageSettings.ApplicationSettings;


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


 this.toggle.Click += new EventHandler<RoutedEventArgs>(toggle_Click);
            this.toggle.Indeterminate += new EventHandler<RoutedEventArgs>(toggle_Indeterminate);
    `}`

void toggle_Unchecked(object sender,RoutedEventArgs e)             {

            this.toggle.Content = "Visibity is off";
            this.toggle.SwitchForeground = new SolidColorBrush(Colors.Red);
            appSettings.Add("value", "off");
        }

        void toggle_Checked(object sender, RoutedEventArgs e)
        {


            this.toggle.Content = "Visibity is on";
            this.toggle.SwitchForeground = new SolidColorBrush(Colors.Green);
            appSettings.Add("value", "on");
        }

        void toggle_Indeterminate(object sender, RoutedEventArgs e)
        {
            //add some content here
        }

        void toggle_Click(object sender, RoutedEventArgs e)
        {
            //add some content here


        }

默认情况下调用checked方法。如果用户取消选中该按钮,那么登录应用程序的用户需要再次显示unchcked,因为用户之前已取消选中btn.but它显示已检查。因为我在隔离存储中保存了一个值。 你能告诉我在哪里可以获得隔离的可变值吗?

2 个答案:

答案 0 :(得分:1)

您似乎没有在ApplicationSettings对象上调用Save方法 请阅读this guide,了解如何使用隔离存储。

要保存设置:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("userData"))
{
settings.Add("userData", "some value");
}
else
{
settings["userData"] = "some value";
}
settings.Save();

要检索设置:

if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
{
 string result  IsolatedStorageSettings.ApplicationSettings["userData"] as string;
}

因此,在您的情况下,将CheckBox的状态保存在Checked&amp; UnChecked事件,并在页面的init中加载状态。

答案 1 :(得分:1)

您可以按照创建它的相同方式访问任何页面中的isolatedstorage值。

尝试在InitializeComponent();

之后访问Settings()构造函数中的值
public Settings()
    {
        InitializeComponent();
        string value;
        if (appSettings.Contains("value"))
        {
            appSettings.TryGetValue("value", out value);
        }

然后您可以根据'值'更改切换按钮的值。

相关问题