gpt4 book ai didi

c# - silverlight、保存负载、IsolatedStorageFile 和 IsolatedStorageFileStream。异常(exception)情况

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:56 26 4
gpt4 key购买 nike

Windows Phone 7 应用该应用程序的目标是一个简单的待办事项列表。我有一个“toditem”类,我将这些对象添加到 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..
}

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

"Attempt to access the method failed: System.Io.streamreader..ctor (System.String)"

然后我得到

Operation not permitted on 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);
}

在公共(public)部分类 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 应用程序。

最佳答案

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

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();
}
}
}

关于c# - silverlight、保存负载、IsolatedStorageFile 和 IsolatedStorageFileStream。异常(exception)情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4230545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com