【问题标题】:Isolated Storage - Windows Phone 8独立存储 - Windows Phone 8
【发布时间】:2015-10-23 20:19:08
【问题描述】:

我的项目中有两个页面。第一个页面即将创建 MyPoint 对象,然后将其添加到 List 中,最后将其保存到独立存储中,如下所示:

IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

积分等级:

   public class MyPoint
    {

        public Ellipse Point { get; set; }
        public string Tag { get; set; }
        public GeoCoordinate Coords { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }

        public MyPoint() { }

        public MyPoint(Ellipse point, GeoCoordinate cords, string tag, string city, string postalCode)
        {
            this.Point = point;
            this.Coords = cords;
            this.Tag = tag;
            this.City = city;
            this.PostalCode = postalCode;
        }
    }

保存到独立存储方法:

private void SaveAppSettings()
        {
            try
            {
                appSettings.Remove("points");
            }
            catch { }
            try
            {
                appSettings.Add("points", this.points);
                appSettings.Save();   // this throws an Exception
            }
            catch { };
        }

Second的页面加载方式

try
        {
            List<MyPoint> points = (List<MyPoint>)appSettings["points"];
            foreach (MyPoint p in points)
            {
                source.Add(new PointsBook(p.Tag, p.Coords, p.City, p.PostalCode));
            }
        }

重点是

appSettings.Save() 引发异常,即使我评论此行一切正常。我已经保存了积分,我可以通过这两页阅读它们。

问题是当我关闭应用程序时,我的 appSetting 有一个字符串“points”,但里面什么都没有。它只有 ["key"] 但没有 [value]。

【问题讨论】:

  • 什么是 appSettings ?在问题中添加所有内容
  • 无论如何我从 MyPoint 属性中删除了 GeoCorrdinate - 仍然无法正常工作

标签: c# windows-phone-8


【解决方案1】:

问题解决了!!

MyPoint 对象在构造函数中有 Ellipse。隔离存储抛出关于 UIElement 无法序列化的异常。

现在一切正常!

【讨论】:

  • MyPoint 看起来属于应用程序的“模型”部分,而椭圆属于“视图”,将它们放在一起并不是一个好主意
猜你喜欢
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多