2012-06-20 22 views
7

我正在將我發佈的應用程序移植到Windows Phone中,以贏取它8.嘗試寫入IsolatedStorage等效項,ApplicationDataContainer時,出現異常。唯一的例外說ApplicationDataCompositeValue的大小

錯誤:狀態管理器設置的大小已超出上限

我不知道這是否是使用ApplicationDataContainer的正確途徑。

public void WriteToIsolatedStorage() 
    { 
     try 
     { 

      ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings; 
      ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue(); 

      if (localSettings.Containers.ContainsKey("LoveCycleSetting")) 
      { 
       localSettings.DeleteContainer("LoveCycleSetting"); 
      } 

      composite["GetWeekStart"] = m_bWeekStart; 

      composite["iHistCount"] = m_iHistCount; 

      composite["dtHistory"] = this.DateTimeToString(m_dtHistory); 

      composite["avgCycleTime"] = m_iAvgCycleTime; 
     } 
    } 

第二行最後一行出現異常。 m_dtHistory是一個大小爲400的字符串數組。ApplicationDataCompositeValue也有固定大小嗎?或者我必須將m_dtHistory數組寫入文件中? WindowsPhone中的Cuz我可以直接將數組寫入IsolatedStorageSettings

如果有人能指導我或給予鏈接,這將是非常有幫助的。

Alfah

+1

什麼是價值[http://msdn.microsoft.com/en-US/library/windows/apps/windows.storage.applicationdata.roamingstoragequota](http://msdn .microsoft.com/en-US/library/windows/apps/windows.storage.applicationdata.roamingstoragequota) –

+0

此錯誤的德語版本可能爲HRESULT 0x80073DC8「DieGrößedes Einstellungswerts des Zustands-Managers hat den den Grenzwertüberschritten」 –

回答

6

是,具有諷刺意味的設置存儲是比手機的WinRT上更容易。您可以將其序列化爲文件。這是我所做的(部分從已在SuspensionManager.cs中的代碼中複製),它適用於值類型和引用類型。

internal static async Task<bool> SaveSetting(string Key, Object value) 
    { 
     var ms = new MemoryStream(); 
     DataContractSerializer serializer = new DataContractSerializer(value.GetType()); 
     serializer.WriteObject(ms, value); 
     await ms.FlushAsync(); 

     // Uncomment this to preview the contents being written 
     /*char[] buffer = new char[ms.Length]; 
     ms.Seek(0, SeekOrigin.Begin); 
     var sr = new StreamReader(ms); 
     sr.Read(buffer, 0, (int)ms.Length);*/ 

     ms.Seek(0, SeekOrigin.Begin); 
     StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(Key, CreationCollisionOption.ReplaceExisting); 
     using (Stream fileStream = await file.OpenStreamForWriteAsync()) 
     { 
      await ms.CopyToAsync(fileStream); 
      await fileStream.FlushAsync(); 
     } 
     return true; 
    } 

    // Necessary to pass back both the result and status from an async function since you can't pass by ref 
    internal class ReadResults 
    { 
     public bool Success { get; set; } 
     public Object Result { get; set; } 
    } 
    internal async static Task<ReadResults> ReadSetting<type>(string Key, Type t) 
    { 
     var rr = new ReadResults(); 

     try 
     { 
      var ms = new MemoryStream(); 
      DataContractSerializer serializer = new DataContractSerializer(t); 

      StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(Key); 
      using (IInputStream inStream = await file.OpenSequentialReadAsync()) 
      { 
       rr.Result = (type)serializer.ReadObject(inStream.AsStreamForRead()); 
      } 
      rr.Success = true; 
     } 
     catch (FileNotFoundException) 
     { 
      rr.Success = false; 
     } 
     return rr; 
    } 
0

我在其他地方見過,但失去了參考的大小是64KB

+1

nope ,它甚至不能處理20KB的數據 – balint

+0

我認爲他是對的,所有值的總數是64千字節。儘管個人價值的最大值可能會小得多。 –