我必须存储此类的对象,
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??
}
我的问题是我无法访问value变量的内容, 有什么帮助吗? 谢谢你的帮助
更新: 感谢Tommaso Scalici的回复,我试图将复合值影响到另一个变量,如下所示:
Windows.Storage.ApplicationDataCompositeValue composite = (Windows.Storage.ApplicationDataCompositeValue)Page1.localSettings.Values["exampleCompositeSetting"];
if (composite != null)
{
int tt = composite["intVal"]; //error here
...
}
else
{
....
}
错误是:
无法隐式转换'Int'中的'object'类型 ”。存在显式转换(cast-is 它不见了?)
UPDATE2: 我在这些方面得到了另一个错误:
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;
信息WinRT:尝试序列化要写入的值时出错 到应用程序数据存储
答案 0 :(得分:1)
那是因为以这种方式你只能存储基本类型(int,string等)。如果要存储复杂对象,则必须使用ApplicationDataCompositeValue(此处为MSDN文档link)
否则,如果类型不是很复杂,您始终可以将实例序列化为JSON并保存序列化字符串。但请记住,简单设置值的限制是8K字节(对于复合设置值为64K字节)。
您要存储的类必须标记为可序列化,如下所示:
使用System.Runtime.Serialization;
[DataContract]
public class Connected
{
[DataMember]
public int Connected { get; set; }
[DataMember]
public bool StateConnection { get; set; }
}