【问题标题】:silverlight, Save load, IsolatedStorageFile and IsolatedStorageFileStream. Exceptionssilverlight、保存负载、IsolatedStorageFile 和 IsolatedStorageFileStream。例外
【发布时间】:2010-11-20 00:25:02
【问题描述】:

Windows Phone 7 应用程序 该应用程序的目标是一个简单的待办事项列表。 我有一个类“todiitem”,我将这些对象添加到 Items 对象中。

在我看来,我正在做一些非常复杂的事情,而且很可能没有干净或体面的代码

但是我对“IsolatedStorageFile”有一些严重的问题

 public class ToDoItem
    {
        public string ToDoName { get; set; } // Add controle's enz.
        public string ToDoDescription { get; set; }
        internal Priority PriortiySelection { get; set; }
...
}

Items 类(基本上是一个包装类,所以我可以访问它)

public class Items
    {
        public static List<ToDoItem> Itemslist = new List<ToDoItem>();
        public static List<ToDoItem> GetList()

        static methods here..
   }

下面的代码返回以下异常:

"尝试访问该方法失败: System.Io.streamreader..ctor (System.String)"

然后我得到了

IsolatedStorageFileSTream 上不允许操作

  if (store.FileExists(@"items.std"))
                {

                    ToDoItem item = new ToDoItem();
                    try
                    {
                        IsolatedStorageFileStream save = new IsolatedStorageFileStream(@"items.std", FileMode.Open, store);
                        BinaryReader reader = new BinaryReader(save);
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message);
                    }

在公共部分类 NewToDo 中:PhoneApplicationPage 我添加了以下方法。它再次返回上述异常,我只假设它是由于某种原因允许的,或者我犯了一些巨大的错误。

 private void saveItem(ToDoItem toDoItem)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(store.OpenFile(@"items.std", FileMode.Append)))
                {
                    sw.WriteLine(toDoItem.ToDoName);
                    sw.WriteLine(toDoItem.ToDoDescription);
                    sw.WriteLine(toDoItem.PriortiySelection.ToString());
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }

如果您需要更多信息,我很乐意提供,我目前是比利时一所大学二年级的学生,我正在玩 windows phone7 应用程序。

【问题讨论】:

    标签: c# silverlight silverlight-4.0 windows-phone-7


    【解决方案1】:

    以下将从隔离存储中读取文件的内容

    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!store.FileExists(VIEW_MODEL_STORAGE_FILE))
        {
            return result;
        }
    
        using (var isfs = new IsolatedStorageFileStream(VIEW_MODEL_STORAGE_FILE, FileMode.Open, store))
        {
            using (var sr = new StreamReader(isfs))
            {
                string lineOfData;
    
                while ((lineOfData = sr.ReadLine()) != null)
                {
                    result += lineOfData;
                }
            }
        }
    }
    

    该示例构建了一个数据字符串 (result)。这实际上是一个序列化的对象,它实际上是其他对象的集合。然后可以将其反序列化回集合。这可能比您尝试将属性一次写入一个文件更可取。

    文件的编写方法如下:

    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var isfs = new IsolatedStorageFileStream(VIEW_MODEL_STORAGE_FILE, FileMode.Create, store))
        {
            using (var sw = new StreamWriter(isfs))
            {
                sw.Write(serializedCollectionObject);
                sw.Close();
            }
        }
    }
    

    【讨论】:

    • 谢谢伙计,我现在可以存储和保存我的对象,即使没有“serializedCollectionObject”,这不起作用?在银光?接下来详细信息。
    【解决方案2】:

    您是否有可能没有处理所有一次性对象并在您第二次尝试访问某个资源时遇到问题,因为它仍在使用中?

    using 语句是一种轻松处理此问题的好方法,更多内容请参见此处。

    Dispose with Using

    这里有更多关于 Jm47 出于这个原因收到相同错误消息的主题的背景信息。

    Problem opening a stream to an isolatedstorage image already the source on an image?

    【讨论】:

    • 我遇到了这个问题 - 我的部分代码忘记调用 fileStream.Close(),所以后来当我的另一部分代码尝试打开文件时,它得到了异常。 (网络版 Silverlight)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多