compose sms task in windows phone 7 c#
I am trying to do a composeSMS task in windows phone 7. And i have a array of data retrieve from isolated storage. How can i make it loop to get all the data into one message. Below is my code but it only get the lastest data.
开发者_开发技巧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();
}
TL;DR: You are constantly setting the message body in the loop instead of appending data to it. For it to work, you need to have:
composeSMS.Body += "Below is my schedule: \n" +
"Date: " + timeList + "\n" +
"Time: " + titleList + "\n" +
"End time: " + categoryList + "\n";
Now, let me point out that your code is not optimized and can generally be shortened. For example, look at this:
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();
}
NOTE: I edited your code in Notepad without having access to a compiler. Some modifications might be needed.
Here are some major differences from what you have.
- I am re-using the same reader (instance of StreamReader) over and over instead of creating new instances for every single file.
- Notice the using statement used along the lines where the reader is used. Also notice curly braces - that way, the reader instance will be properly disposed once I am done working with it (reading a file).
- You only need one instance of a placeholder string, since all composite line strings are never used anywhere. That placeholder string is reset with every read.
- I am using a StringBuilder to build the body and only when it's ready, I am setting the Body property.
- No need for intermediary variables inside the loop.
Last but not least - a very important advice. You need to refactor the way you are storing data. Reading four files in this case to build a single entity is wrong and resource-consuming. Consider using a single well-structured format (e.g. XML or JSON) to store the data in a single file. Even better, consider using a database (starting SDK v.7.1 there is support for SQL CE).
精彩评论