开发者

Retrieving data from isolated storage windows phone 7

I am trying to retrieve all the data in my isolated storage alarm.txt. The format of the data saved inside is like this not^no^home^apple^hao^how^.... and so on. And then i will put the split data "not" "no" "home" (3 by 3 and so on) into library items for data binding.

For my below code i only managed to get the first 3 data. What should i do to let it continue looping?

string[] alarmDetailsSeparated;

        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 开发者_StackOverflow(Exception)
        {
        }

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


Your problem can be solved very easily by using the real power of the .NET framework. This way you get rid of the ugly way you currently represent your data.

First of all, create a class to hold your alarm data:

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

Now we create a class that serves as a storage provider to the XML file:

/// <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);
        }
    }
}

Now use this the storage object to load and save the alarms on your page:

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

Now you need to bind the properties of an Alarm object to controls. After this, add a [Save] button somewhere in your UI an call the Save() method of the storage class:

storage.Save(myAlarms);

This is it! A simple and clean solution! To be honest, I was not able to test the whole code, but this should work...


You're only reading the first line. Try:

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

Full Sample:

        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;
    }
}

}


You've got an error in your For statement.

Change

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

to

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


Try this:

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],
                    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜