Windows Phone 8.1中的本地设置不允许我将对象指定为值c#

时间:2015-05-23 23:21:38

标签: c# windows-phone-8.1

我创建了一个类来保存数据条目,当我尝试将类分配给windows phone 8.1中的本地设置值时,它会不断抛出系统异常。这是代码任何帮助将不胜感激。谢谢

  public static HoursList GetHoursList()
    {
        Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
        HoursList h = (HoursList)localSettings.Values["HoursList"];
        if (h == null)
        {
            localSettings.Values["HoursList"] = new HoursList(); //This is where the exception throws
        }
        return (HoursList)localSettings.Values["HoursList"];

1 个答案:

答案 0 :(得分:2)

您不能以这种方式存储复杂类型。如何解决这个问题的选择很少。如果您的类HoursList很复杂,那么您可以将其序列化 - http://www.codeproject.com/Articles/1789/Object-Serialization-using-C

最简单的方法是让其他人(如Newstonsoft https://www.nuget.org/packages/newtonsoft.json/)进行序列化和反序列化工作。

如果类足够简单,你可以只构建字符串,包括例如;作为分隔符并保存。 String.Join()yourString.split()会对您有所帮助。