【问题标题】:WP7 Saving data in isolated storageWP7 将数据保存在隔离存储中
【发布时间】:2013-02-15 12:55:09
【问题描述】:

我将数据保存在隔离存储中,但是当我重新启动手机时,我无法从那里读取这些数据。隔离存储为空。为什么?

如果我不关手机一切正常

这是我的代码:

 using (Stream file = IsolatedStorageHelper.OpenFile(USER_ACCOUNT_FILE, fileMode.Create))
        {
            if (null != file)
            {
                try
                {
                    XDocument xml = new XDocument();
                    XElement root = new XElement("userAccount");

                    root.Add(new XAttribute("FirstName", this._firstName));
                    root.Add(new XAttribute("LastName", this._lastName));
                    root.Add(new XAttribute("ID", this._id));
                    root.Add(new XAttribute("Sex", this._sex));

                    xml.Add(root);

                    // save xml data
                    xml.Save(file);
                }
                catch
                {
                }
            }
        }

在隔离存储中创建文件的功能

static public IsolatedStorageFileStream OpenFile(string aFilename, FileMode mode)
            {
                IsolatedStorageFileStream res = null;

                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {

                    try
                    {
                        res = new IsolatedStorageFileStream(aFilename, mode, FileAccess.ReadWrite, isoStore);
                    }
                    catch (Exception exc)
                    {
                        if ((null != (exc as IsolatedStorageException)) &&
                            (FileMode.Open != mode) &&
                            (true == createPathOnIsolatedStorage(isoStore,aFilename)) )
                        {
                            try
                            {
                                res = new IsolatedStorageFileStream(aFilename, mode, isoStore);
                            }
                            catch
                            {
                            }
                        }
                    }
                }

                return res;
            }

【问题讨论】:

  • 你是用真机还是模拟器?
  • 应用启动时会发生什么?是否调用了 OpenFile()?您应该为 OpenFile 使用的 FileMode 应该是 FileMode.OpenOrCreate,而不是 FileMode.Open。 FileMode.Open 将始终重新创建文件并覆盖您之前拥有的任何数据。
  • 我在模拟器上测试过。我已经在手机上运行应用程序,一切都开始工作了。谢谢

标签: c# windows-phone-7


【解决方案1】:

如果您说的是在模拟器上运行它,这是正常行为。默认情况下,模拟器不保留隔离存储。

物理设备将始终将数据保存在存储中,除非明确重置、卸载应用程序或用户通过应用程序提供的一种方式删除内容。

【讨论】:

  • 谢谢。你是对的,我在模拟器上测试了应用程序,当我在手机上测试时,一切都开始正常了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多