0

我的圖表爲完全新的領域在這一個...取數據從Azure的移動服務返回並寫入XML文件中IsolatedStorage

目前,我使用Azure的移動服務和SQL Azure數據庫存儲我的Windows Phone 8應用程序的數據。每次啓動應用程序時,都會通過我設置的一些查詢來從特定表格中提取所有數據。

items = await phoneTable 
    .Where(PhoneItem => PhoneItem.Publish == true) 
    .OrderBy(PhoneItem => PhoneItem.FullName) 
    .ToCollectionAsync(); 

但是,這並不總是一個很好的做法。我試圖實現一種方式,將數據保存到應用程序已加載的IsolatedStorage中的XML文件中。

我已經收到了一些代碼,我認爲應該閱讀IsolatedStorage並搜索XML文件,但我不確定如何下載數據,然後將其寫入IsolatedStorage

public static IEnumerable<Phones> GetSavedData() 
    { 
     IEnumerable<Phones> phones = new List<Phones>(); 

     try 
     { 
      using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       string offlineData = Path.Combine("WPTracker", "Offline"); 

       string offlineDataFile = Path.Combine(offlineData, "phones.xml"); 

       IsolatedStorageFileStream dataFile = null; 

       if (store.FileExists(offlineDataFile)) 
       { 
        dataFile = store.OpenFile(offlineDataFile, FileMode.Open); 

        DataContractSerializer ser = new DataContractSerializer(typeof(IEnumerable<Phones>)); 

        phones = (IEnumerable<Phones>)ser.ReadObject(dataFile); 

        dataFile.Close(); 
       } 
       else 
       { 
        // Call RefreshPhoneItems(); 

       } 
      } 
     } 
     catch (IsolatedStorageException) 
     { 

     } 

     return phones; 
    } 

我使用的AzureMobileServices SDKNewtonsoft.Json與數據庫進行交互。任何幫助將不勝感激!

回答

0

在這種情況下不要使用ToCollectionAsync - 它將返回一個對象,該對象在綁定到某個UI控件時最適用。改爲使用ToListAsync,有點像下面的代碼:

items = await phoneTable 
    .Where(PhoneItem => PhoneItem.Publish == true) 
    .OrderBy(PhoneItem => PhoneItem.FullName) 
    .ToListAsync(); 

using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    string offlineData = Path.Combine("WPTracker", "Offline"); 
    string offlineDataFile = Path.Combine(offlineData, "phones.xml"); 
    IsolatedStorageFileStream dataFile = null; 
    dataFile = store.OpenFile(offlineDataFile, FileMode.Create); 
    DataContractSerializer ser = new DataContractSerializer(typeof(IEnumerable<Phones>)); 
    ser.WriteObject(dataFile, items); 
    dataFile.Close(); 
} 
+0

@carlosfiguerira現在我將不得不改變我的文本框如何接收數據?目前我只是將'items'綁定到'ListBox'。我現在必須將ListBox綁定到XML文件嗎? –

相關問題