如何访问本地存储中值变量的内容

How to access the content of value variable in Local Storage

我必须存储这个 Class,

的对象

Page1.cs:

public class Connected
    {
        public static int connected;
        public static RootObject rootObjectCnx;
    }

但问题是我无法访问值变量内容,这是我的尝试:

 public static Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
....
  Connected c = new Connected();
  saveData(c);
...
     private void SaveData(Connected c)
            {
                localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
                localSettings.Values["exampleSetting"] = c;
            }

然后我是这样读的:

Page2.cs:

对象值 = Page1.localSettings.Values["exampleSetting"];

    if (value != null)
    {

       //how can I access the value variable??

    }

我的问题是我无法访问值变量的内容, 有什么帮助吗?? 感谢帮助

更新: 感谢 Tommaso Scalici 的回复,我试图将复合值影响到另一个变量,如下所示:

Windows.Storage.ApplicationDataCompositeValue composite =          (Windows.Storage.ApplicationDataCompositeValue)Page1.localSettings.Values["exampleCompositeSetting"];
 if (composite != null)
            {
                int tt = composite["intVal"]; //error here

               ...
            }

            else
            {
               ....
            }

错误是:

Can not implicitly convert type 'object' in 'Int '. An explicit conversion exists (a cast-is it missing?)

更新2: 我在这些行收到另一个错误:

  ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue();
            composite["boolVal"] = myClassCnx.stateConnexion;(this variable is a bool)
            composite["intVal"] = Connected.connected; (this is an int variable)
            localSettings.Values["exampleCompositeSetting"] = composite;

Informations WinRT : Error trying to serialize the value to be written to the application data store

那是因为那样你只能存储原始类型(int、string 等等)。如果要存储复杂对象,则必须使用 ApplicationDataCompositeValue(此处为 MSDN 文档 link

否则,如果类型不是很复杂,您可以随时将实例序列化为 JSON 并保存序列化字符串。但请记住,简单设置值的限制为 8K 字节(复合设置值限制为 64K 字节)。

您要存储的 class 必须像这样标记为可序列化:

使用 System.Runtime.Serialization;

[DataContract]
public class Connected
{
    [DataMember]
    public int Connected { get; set; }

    [DataMember]
    public bool StateConnection { get; set; }
}