从隔离存储窗口电话7中检索数据

时间:2011-07-12 11:36:33

标签: windows-phone-7 isolatedstorage

我正在尝试检索隔离存储器alarm.txt中的所有数据。 保存在里面的数据的格式就像这样没有^没有^ home ^ apple ^ hao ^如何^ ....等等。 然后我将拆分数据“不”“不”“主页”(3乘3等)放入库项目进行数据绑定。

对于我的下面代码,我只设法获得前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;
        }
    }
}

4 个答案:

答案 0 :(得分: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;

现在需要将Alarm对象的属性绑定到控件。在此之后,在UI中的某处添加[Save]按钮,调用存储类的Save()方法:

storage.Save(myAlarms);

就是这样!简单而干净的解决方案! 说实话,我无法测试整个代码,但这应该有用......

答案 1 :(得分: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;
    }
}

}

答案 2 :(得分:0)

你的For语句中有错误。

更改

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

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

答案 3 :(得分: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],
                    });
}