【问题标题】:How can I store List<T> in local settings?如何将 List<T> 存储在本地设置中?
【发布时间】:2015-12-16 09:47:46
【问题描述】:

目前我正在使用以下方法来存储编译成功的数据

App.removalList.Add(new RemoveFavourites(App.user.auth_token, App.user.user_id, proid.ToString()));

ApplicationData.Current.LocalSettings.Values["Remove_fav_properties_list"] = App.removalList;

它编译成功但是我在运行时收到以下错误:

WinRT 信息:尝试序列化要写入应用程序数据存储的值时出错。
其他信息:不支持此类型的数据。

以下是 RemoveFavourites 类:

public class RemoveFavourites
{

    public string auth_token { get; set; }

    public string user_id { get; set; }

    public string property_id { get; set; }

    public RemoveFavourites(string auth_token, string user_id, string property_id)
    {
        this.auth_token = auth_token;
        this.user_id = user_id;
        this.property_id = property_id;
    }
}

完全的例外是

“System.Exception”类型的异常发生在 mscorlib.ni.dll 中,但未在用户代码中处理 WinRT 信息:尝试序列化要写入应用程序数据存储的值时出错 附加信息:不支持这种类型的数据。 尝试序列化要写入应用程序数据存储的值时出错 如果有这个异常的处理程序,程序可以安全地继续。

【问题讨论】:

  • 您应该能够获取内部异常详细信息以检查哪个属性RemoveFavourites 类未成功序列化。有些类型,比如SolidColorBrush,是不可序列化的。
  • 请显示您正在使用的T的实际类型的定义;问题可能是由特定类型引起的。请同时在配置文件中显示列表的定义。
  • 感谢 nvartak 和 codor 的快速回复,我已经更新了问题。
  • 你试过App.removalList.ToArray()吗?
  • 是的,我做到了,它也给了我同样的错误。

标签: c# .net list windows-phone-8.1


【解决方案1】:

您不能在应用设置中存储复杂的对象。 (reference)

尝试序列化对象(可能使用json或您自己实现的自定义序列化方式),然后存储数据。

【讨论】:

  • 您完全确定在 Visual Studio 的 IDE 中配置是不可能的还是只是不可能?
  • 见参考@Codor
  • 感谢您的提示;请注意,this answer 建议使用自定义格式来存储用户定义类型的对象(这可以被视为将一种机制“插入”到另一种机制中)。
猜你喜欢
  • 2021-06-28
  • 1970-01-01
  • 2013-02-18
  • 2019-10-19
  • 1970-01-01
  • 2020-08-10
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多