编写短信任务在Windows phone 7 c#

本文关键字:Windows phone 任务 信任 | 更新日期: 2023-09-27 18:04:22

我正在尝试在windows phone 7中执行composeSMS任务。我有一组从独立存储中检索到的数据。我怎样才能使它循环得到所有的数据到一个消息。下面是我的代码,但它只得到最新的数据。

private void sendSmsBtn_Click(object sender, RoutedEventArgs e)
{
    try
    {
        //For sorted time
        StreamReader readFileTime = new StreamReader(new IsolatedStorageFileStream(fullFolderName + "''time.Schedule", FileMode.Open, myStore));
        //For time
        StreamReader readFileTime1 = new StreamReader(new IsolatedStorageFileStream(fullFolderName + "''time1.Schedule", FileMode.Open, myStore));
        //For title
        StreamReader readFileTitle = new StreamReader(new IsolatedStorageFileStream(fullFolderName + "''title.Schedule", FileMode.Open, myStore));
        //For category
        StreamReader readFileCategory = new StreamReader(new IsolatedStorageFileStream(fullFolderName + "''category.Schedule", FileMode.Open, myStore));

        //Sorted time list
        String timeText = readFileTime.ReadLine();
        timeSplit = timeText.Split(new char[] { '^' });
            Array.Sort(timeSplit, delegate(string first, string second)
        {
            return DateTime.Compare(Convert.ToDateTime(first), Convert.ToDateTime(second));
        });

        String timeText1 = readFileTime1.ReadLine();
        timeSplit1 = timeText1.Split(new char[] { '^' });
        //Array.Sort(timeSplit1);
        String titleText = readFileTitle.ReadLine();
        titleSplit = titleText.Split(new char[] { '^' });
        Array.Sort(titleSplit);
        String categoryText = readFileCategory.ReadLine();
        categorySplit = categoryText.Split(new char[] { '^' });
        Array.Sort(categorySplit);
    }
    catch (Exception)
    {
    }

    SmsComposeTask composeSMS = new SmsComposeTask();
        for (int i = 0; i < timeSplit.Length; i++)
        {
            timeList = timeSplit[i];
            titleList = titleSplit[i];
            categoryList = categorySplit[i];

            composeSMS.Body = "Below is my schedule: 'n" +
                              "Date: " + timeList + "'n" +
                              "Time: " + titleList + "'n" +
                              "End time: " + categoryList + "'n";
        }
        composeSMS.Show();
}

编写短信任务在Windows phone 7 c#

TL;DR:您不断地在循环中设置消息体,而不是向其添加数据。要使其工作,您需要有:

composeSMS.Body += "Below is my schedule: 'n" +
                 "Date: " + timeList + "'n" +
                 "Time: " + titleList + "'n" +
                 "End time: " + categoryList + "'n";

现在,让我指出你的代码没有优化,通常可以缩短。例如,看看这个:

private void sendSmsBtn_Click(object sender, RoutedEventArgs e)
{
    try
    {
        StreamReader reader;
        string[] timeSplit;
        string[] timeSplit1;
        string[] titleSplit;
        string placeholder;
        string[] categorySplit;
        //For sorted time
        using (reader = new StreamReader(new IsolatedStorageFileStream(fullFolderName + "''time.Schedule", FileMode.Open, myStore))
        {
            placeholder = reader.ReadLine();
        }
        timeSplit = placeholder.Split(new char[] { '^' });
        Array.Sort(timeSplit, delegate(string first, string second)
        {
           return DateTime.Compare(Convert.ToDateTime(first), Convert.ToDateTime(second)); 
        }); 
        using (reader = new StreamReader(new IsolatedStorageFileStream(fullFolderName + "''time1.Schedule", FileMode.Open, myStore))
        {
            placeholder = reader.ReadLine();
        }
        timeSplit1 = placeholder.Split(new char[] { '^' });
        Array.Sort(titleSplit1);
        using (reader = new StreamReader(new IsolatedStorageFileStream(fullFolderName + "''title.Schedule", FileMode.Open, myStore)))
        {
            placeholder = reader.ReadLine();
        }
        titleSplit = placeholder.Split(new char[] { '^' });
        Array.Sort(titleSplit);
        using(reader = new StreamReader(new IsolatedStorageFileStream(fullFolderName + "''category.Schedule", FileMode.Open, myStore)))
        {
            placeholder = readFileCategory.ReadLine();
        }
        categorySplit = placeholder.Split(new char[] { '^' });
        Array.Sort(categorySplit);
    }
    catch (Exception)
    {
    }
    var composeSMS = new SmsComposeTask();
    var sBuilder = new StringBuilder();
    sBuilder.AppendLine("Below is my schedule:");
    for (int i = 0; i < timeSplit.Length; i++)
    {
        sBuilder.AppendLine("Date: " + timeSplit[i]);
        sBuilder.AppendLine("Time: " + titleSplit[i]);
        sBuilder.AppendLine("End time: " + categorySplit[i]);
    }
    composeSMS.Body = sBuilder.ToString();
    composeSMS.Show();
}

注意:我在记事本中编辑了您的代码,而没有访问编译器。可能需要做一些修改。

这里有一些主要的不同之处。

  • 我一遍又一遍地重复使用相同的阅读器 (StreamReader的实例),而不是为每个文件创建新的实例。注意using语句在使用读取器的行中使用。还要注意花括号——这样,一旦我完成了对阅读器实例的处理(读取文件),它将被正确地处理。
  • 您只需要一个占位符字符串的实例,因为所有复合行字符串在任何地方都不会使用。占位符字符串在每次读取时重置。
  • 我正在使用StringBuilder来构建主体,只有当它准备好时,我才设置 body 属性。
  • 循环中不需要中间变量

最后但并非最不重要——一个非常重要的建议。需要重构存储数据的方式。在这种情况下,读取四个文件来构建单个实体是错误的,而且会消耗资源。考虑使用单一结构良好的格式(例如XML或JSON)将数据存储在单个文件中。更好的做法是,考虑使用数据库(从SDK v.7.1开始就支持SQL CE)。