2010-11-20 62 views
2

Windows Phone 7應用程序 應用程序的目標是一個簡單的待辦事項列表。 我有一個類'toditem'我將這些對象添加到Items對象。silverlight,保存加載,IsolatedStorageFile和IsolatedStorageFileStream。異常

在我看來,我做的事情非常複雜,最有可能不乾淨或像樣的代碼

但我有一些嚴重的問題,「IsolatedStorageFile」

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

項目類(basicly包裝CLAS所以我可以存取權限的話)

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應用玩耍。

回答

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

由於有隊友,現在我能夠存儲和保存我的對象艱難吧沒有「serializedCollectionObject」這也不行?在silverlight? 接下來是詳細信息。 – Syneryx 2010-11-20 17:00:43

1

難道你不處置的所有可支配的對象,並遇到一個問題,因爲它仍然在使用?

using語句是一個很好的方法來處理這個問題,更多的在這裏。在話題在這兒,Jm47居然也得到了同樣的錯誤消息,因爲這個原因更

Dispose with Using

有點背景。

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

+0

我有這個問題 - 我的代碼的一部分忘記調用fileStream.Close(),所以後來當我的代碼的另一部分試圖打開文件時,它得到了異常。 (Silverlight for Web) – CodeThug 2011-01-16 04:35:39