2011-07-12 79 views
1

我想要檢索我孤立的存儲alarm.txt中的所有數據。 裏面保存的數據格式就是這樣不是^ no^home^apple^hao ^怎麼^ ......等等。 然後我會將分割數據「不」,「不」,「家」(3乘3等等)放入用於數據綁定的庫項目中。從孤立的存儲窗口檢索數據電話7

對於我的下面的代碼,我只設法得到前3個數據。 我該怎麼做讓它繼續循環?

string [] alarmDetailsS​​eparated;

 var isoFile = IsolatedStorageFile.GetUserStoreForApplication(); 

     IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
     try 
     { 
      StreamReader readFile = new StreamReader(new IsolatedStorageFileStream("AlarmFolder\\alarm.txt", FileMode.Open, myStore)); 

      String fileText = readFile.ReadLine(); 

      //alarmDetailsSeparated is the array that hold the retrieved alarm details from alarm.txt and is split by '^' 
      alarmDetailsSeparated = fileText.Split(new char[] { '^' }); 
      ObservableCollection<Items> LibraryItems = new ObservableCollection<Items>(); 

       for (int i = 0; i < alarmDetailsSeparated.Length;) 
       { 
        if (test > 0) 
        { 
         i = test; 
        } 
        //To add the alarmDetailsSeparated into the alarmListBox 
        dateSeparate = alarmDetailsSeparated[i]; 
        timeSeparate = alarmDetailsSeparated[i + 1]; 
        labelSeparate = alarmDetailsSeparated[i + 2]; 

        date = dateSeparate; 
        time = timeSeparate; 
        label = labelSeparate; 

        test = test + 3; 
        break; 
       } 
       LibraryItems.Add(new Items(time, label)); 
       alarmListBox.ItemsSource = LibraryItems; 
      } 

     catch (Exception) 
     { 
     } 

     if (alarmListBox.Items.Count == 0) 
     { 
      noAlarmTxtBlock.Visibility = Visibility.Visible; 
     } 
    } 
} 
+1

對不起,但你有沒有想過使用XML而不是這個極端ulgy「不^ ^家^蘋果^好^如何^ ...「自定義格式? – CodeZombie

+0

嗯如何去做呢? –

+0

請參閱http://stackoverflow.com/questions/6663607/reading-complex-xml-in-c-windows-phone –

回答

1

您的問題可以通過使用.NET框架的真正力量很容易得到解決。這樣你就擺脫了你目前代表數據的醜陋方式。

首先,創建一個類來保存您的報警數據:

/// <summary> 
/// Class that holds the information of an alarm. 
/// </summary> 
class Alarm 
{ 
    public string Label { get; set; } 
    public DateTime Time { get; set; } 
} 

現在我們創建作爲存儲提供商的XML文件中的類:

/// <summary> 
/// Class that works as a storage provider to the XML file in the isolated storage 
/// </summary> 
class Storage 
{ 
    public const string StorageFileName = "Alarms.xml"; 

    /// <summary> 
    /// Loads alarms from the storage 
    /// </summary> 
    /// <returns>List of alarms</returns> 
    public ObservableCollection<Alarm> Load() 
    { 
     ObservableCollection<Alarm> alarms = new ObservableCollection<Alarm>(); 

     IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication(); 

     if(!storage.FileExists(StorageFileName)) 
     { 
      return alarms; 
     } 

     using(var stream = storage.OpenFile(StorageFileName, System.IO.FileMode.Open)) 
     { 
      XElement root = XElement.Load(stream); 

      foreach(var alarmElement in root.Elements("alarm")) 
      { 
       alarms.Add(
        new Alarm 
        { 
         Label = alarmElement.Element("label").Value, 
         Time = DateTime.Parse(alarmElement.Element("time").Value) 
        } 
       ); 
      } 
     } 

     return alarms; 
    } 

    /// <summary> 
    /// Saves alarms to the storage file (XML) 
    /// </summary> 
    /// <param name="alarms">Alarms to save</param> 
    public void Save(ObservableCollection<Alarm> alarms) 
    { 
     IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication(); 

     using(var stream = storage.OpenFile(StorageFileName, System.IO.FileMode.Create)) 
     { 
      XElement root = new XElement("alarms"); 

      // Build 
      foreach (var alarm in alarms) 
      { 
       root.Add(
        new XElement("alarm", 
         new XElement("label", alarm.Label), 
         new XElement("time", alarm.Time) 
        ) 
       ); 
      } 

      root.Save(stream); 
     } 
    } 
} 

現在用這個存儲對象加載並保存在您的網頁上的報警:

var storage = new Storage(); 
var myAlarms = storage.Load(); 
alarmListBox.ItemsSource = myAlarms; 

現在你需要綁定屬性o f要控制的報警對象。在此之後,在UI的地方添加【保存】按鈕的調用存儲類的Save()方法:

storage.Save(myAlarms); 

這就是它!簡單而乾淨的解決方案! 說實話,我沒有能夠測試整個代碼,但這應該工作...

0

您只是在閱讀第一行。嘗試:

string fileText; 
while ((fileText = readFile.ReadLine()) != null) 
{ 
.... 
} 

全樣本:

 var isoFile = IsolatedStorageFile.GetUserStoreForApplication(); 

    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
    try 
    { 
     StreamReader readFile = new StreamReader(new IsolatedStorageFileStream("AlarmFolder\\alarm.txt", FileMode.Open, myStore)); 

     ObservableCollection<Items> LibraryItems = new ObservableCollection<Items>(); 

     string fileText; 
     while ((fileText = readFile.ReadLine()) != null) 
     { 
      //alarmDetailsSeparated is the array that hold the retrieved alarm details from alarm.txt and is split by '^' 
      alarmDetailsSeparated = fileText.Split(new char[] { '^' }); 

      for (int i = 0; i < alarmDetailsSeparated.Length;) 
      { 
       if (test > 0) 
       { 
        i = test; 
       } 
       //To add the alarmDetailsSeparated into the alarmListBox 
       dateSeparate = alarmDetailsSeparated[i]; 
       timeSeparate = alarmDetailsSeparated[i + 1]; 
       labelSeparate = alarmDetailsSeparated[i + 2]; 

       date = dateSeparate; 
       time = timeSeparate; 
       label = labelSeparate; 

       test = test + 3; 
       break; 
      } 
      LibraryItems.Add(new Items(time, label)); 
     }   
     alarmListBox.ItemsSource = LibraryItems; 
    } 

    catch (Exception) 
    { 
    } 

    if (alarmListBox.Items.Count == 0) 
    { 
     noAlarmTxtBlock.Visibility = Visibility.Visible; 
    } 
} 

}

+0

我應該在哪裏放? 下面的String fileText = readFile.ReadLine(); ?? –

+0

我試過下面還是隻能得到第一個數據.. String fileText; 而((FILETEXT = readFile.ReadLine())!= NULL){ // alarmDetailsS​​eparated是從alarm.txt保存檢索到的報警的細節和由 '^' alarmDetailsS​​eparated = fileText.Split分裂陣列(new char [] {'^'}); ObservableCollection LibraryItems = new ObservableCollection (); ...... } –

+0

試試我的更新回答。 –

0

你已經在你的發言得到了一個錯誤。

變化

for (int i = 0; i < alarmDetailsSeparated.Length;) 

for (int i = 0; i < alarmDetailsSeparated.Length; i++) 
+0

已更改,但仍然是相同的..我無法弄清楚是什麼問題 –

+0

爲我++ .. 我得到一個警告,說我 - 無法訪問的代碼檢測 –

0

試試這個:

var fileText = "date1^time1^label1^date2^time2^label2^date3^time3^label3^date4^time4^label4"; 

var alarmDetailsSeparated = fileText.Split(new [] { '^' }); 

var output = new ObservableCollection<Items>(); 

for (var i = 0; i < alarmDetailsSeparated.Length; i = i + 3) 
{ 
    output.Add(new Items 
        { 
         Date = alarmDetailsSeparated[i], 
         Time = alarmDetailsSeparated[i + 1], 
         Label = alarmDetailsSeparated[i + 2], 
        }); 
}