0

我從隔離儲存讀取對象用下面的代碼:錯誤讀取序列化對象? WP7

public static T Load<T>(string name) where T : class, new() 
    { 
     T loadedObject = null; 
     using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication()) 
     using (IsolatedStorageFileStream storageFileStream = new IsolatedStorageFileStream(name, System.IO.FileMode.OpenOrCreate, storageFile)) 
     { 
      if (storageFileStream.Length > 0) 
      { 
        DataContractSerializer serializer = new DataContractSerializer(typeof(T)); 
        loadedObject = serializer.ReadObject(storageFileStream) as T; //####Error Here#### 
      } 
      if (loadedObject == null) 
      { 
       loadedObject = new T(); 
      } 
     } 

在閱讀我的目標我得到一個SecurityException讀取

「類型「Microsoft.Xna.Framework.Media。宋'不能部分信任反序列化,因爲它沒有公共無參數構造函數。「

正在讀取的我的對象類包含一個引發上述錯誤的Song屬性。

有沒有辦法解決這個問題?我想讓我的Song屬性與我的對象一起存儲。任何建議將不勝感激!謝謝!

回答

0

你的目標是什麼?

  1. 您是否想在應用程序中播放歌曲?
  2. 你是否在應用中創建歌曲?
  3. 你想訪問歌曲庫嗎?
  4. 你想從網上得到這首歌嗎?

@ 1只需在項目中爲您的項目添加一首歌。不需要使用序列化。
@ 2使用內存流來維護它。保存並加載流。使用MediaElement.SetSource(流流)
@ 3使用MediaPlayerLauncher
@ 4宋歌= Song.FromUri ..

您需要將其存儲在不同勢的方式。在這種情況下,例外的解釋幾乎都是爲什麼一首歌是不好的選擇。

看看這個例子。 http://social.msdn.microsoft.com/Forums/en/windowsphone7series/thread/3080021c-3960-49a0-ac5b-ebf2680592e1

+0

我正在存儲一個包含Song屬性的對象。當我從獨立存儲中讀取對象時,我需要讀取Song屬性。 – andersra 2010-12-06 01:42:18