In this tutorial we are going to to finish the statistics tracker by making some last minute enhancements as we prepare the application for market.  In the previous post we implemented Isolated Storage to store data about our statistics and in this tutorial we will draw upon the file we use in isolated storage. To make this application presentable and ready for market, we are going to remove the button that is used to delete the isolated storage file, add the ability to show the total number of counted items when a button is pressed which will be a two step process as well as providing the same number at the bottom of the application and update it as the values are added.

To begin with we are going to need to get rid of the button that allows the database to be removed and remade when pressed.  To do this we are going to comment out our Delete DB button and the code behind it in MainPage.XAML.CS. To do this we need to higlight the code below and navigate to the toolbars and choose the one that looks like this: .  When we return to the XAML code we will need to dispose of the code that makes the button visual on the phone and as such, the second peice of code below will show you what needs to be commented out. After this is done the visual of the Delete DB button will be gone and the code and the visual should look like this:

//private void btnDelete_Click(object sender, RoutedEventArgs e)
//{
// IsolatedStorageFile directory = IsolatedStorageFile.GetUserStoreForApplication();
// if (directory.FileExists("statistics.txt"))
// directory.DeleteFile("statistics.txt");
//}

<!--<Button Content="Delete DB" Height="72" Margin="224,291,6,0" Name="btnDelete" VerticalAlignment="Top" Click="btnDelete_Click" />-->

After this is done we are going to to need to add in another button so we can view the number of statistics earned in a toast notification when a button is pressed. To do this we need to add in a new Button where the old button resided.  We are going to name it btnCountTotal and name the text “View Total“.  After this is done we are going to double click on the button in the Visual View so that we can create the event handler for this button when clicked.  When you are in the code view after we have clicked the button we need to make a toast notification to appear whenever this button is pressed.  To do this we are going to use the MessageBox command to make the the event handler look ike this:

private void btnCountTotal_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(count.ToString());
}

After the MessageBox is added we need to add in a TextBlock at the bottom of the page to be able to update the number of statistics we have acquired, without having to click the “View Total” button all the time. To accomplish this we are going to need to place the TextBlock just under the ListBox (which will require making the ListBox slightly smaller) and we can use the following code for this:

<ListBox HorizontalAlignment="Left" Margin="0,369,0,28" Name="lbEntries" Width="456" IsEnabled="True" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="-12,577,0,0" Name="tbTotalResult" Text="" VerticalAlignment="Top" Width="468" />

After this is accomplished we need to add in the code to update the TextBlock when the button to add the results to the Isolated Storage is clicked.  To do this we will make use of the count variable to reference the number of accumulated statistics and present it in a small sentence denoting the total. We need to make the count variable outside all methods so that the variable will be consistent throughout the applications and would also afford use the opportunity to manipulate the data if need be. To do this we will need to use the following at the top of our class and not inside any method or function:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;

namespace WP7StatsTracker
{
public partial class MainPage : PhoneApplicationPage
{
int count;
// Constructor
public MainPage()
{
InitializeComponent();
}

In line 20 of the code above is the variable we are creating and that will hold our data within the application and be able to give us an accurate total. Afterwards we will need to modify the btnSubmit_Click event handler to handler drawing upon the isolated storage file and to serve all entries into an array so that we may count the number of total statistics in our file. After we have all of the data in an array we can loop through adding all of the items into the count variable we created above. When we add to the count variable we make sure the variable is not containing anything beforehand so we will empty it by making it equal zero. Now we need to make the TextBlock we created have some text in it when we press the Submit button. All of the described content above is coded below for convenience our convenience:

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile directory = IsolatedStorageFile.GetUserStoreForApplication();
if(directory.FileExists(“statistics.txt”))
{
using (var myFilestream = new IsolatedStorageFileStream(“statistics.txt”, FileMode.Append, IsolatedStorageFile.GetUserStoreForApplication()))
using (var writer = new StreamWriter(myFilestream))
{
writer.Write(txtNumRew.Text + “-” + BirthDate.Value.Value.ToShortDateString() + “,”);
}

using (var myFileStream = new IsolatedStorageFileStream(“statistics.txt”, FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication()))
using (var reader = new StreamReader(myFileStream))
{
var text = reader.ReadToEnd();
string texy = “”;
string[] arr = text.Split(‘,’);
count = 0;
for (int i = 0; i < arr.Length; ++i) { texy += arr[i] + "\n"; lbEntries.Items.Add(arr[i]); string[] rew = arr[i].Split('-'); string rewData = rew[0]; if (rewData == "" || rewData == " " || rewData == "0") { } else count += Convert.ToInt32(rewData); } //MessageBox.Show(Convert.ToString(count)); } tbTotalResult.Text = ""; tbTotalResult.Text = "Total Number of rewards is " + count.ToString(); } else if (!directory.FileExists("statistics.txt")) { MessageBox.Show("There is no database, let me create that for you!"); using (var myFilestream = new IsolatedStorageFileStream("statistics.txt", FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication())); } txtNumRew.Text = ""; } [/sourcecode] After this is done we should have a product that looks like the image below when the submit button is pressed.

Now that we have a statistics tracker ready for market we can start thinking about actually publicising it to the masses! To do this, in the next post, we are going to access Microsofts App Hub to do this. If you would like to get ahead before the next post then please explore the site and get yourself familiar with it. If anyone requires help completing this tutorial feel free submitting a comment below and I will answer as soon as I can. Until the next time, Happy Hacking.

<– Isolated Storage Off To Market! –>
Share This