CS387 Mobile Application Development

Windows Phone Programming Practice Sheet 2

Building a Persistent Count Application

Purpose:

To build a simple application that counts as you tap (press the left Mouse Button) and save the current value in isolated storage if you navigate away from the application. When you navigate back to the application, you continue based on the latest value of your count. To reset the count to zero, use a “Reset” button.

Interface:

You need three Grid Rows:

The top row for the application name and page title

The middle row for the count (use a huge font size)

The third row for the button

The following XAML code will help you achieve that:

<!--LayoutRoot is the root grid where all page content is placed-->

Grid x:Name="LayoutRoot" Background="Transparent">

Grid.RowDefinitions

RowDefinition Height="Auto"/>

RowDefinition Height="*"/>

RowDefinition Height="Auto"/>

</Grid.RowDefinitions

<!--TitlePanel contains the name of the application and page title-->

StackPanel x:Name="TitlePanel"Grid.Row="0" Margin="12,17,0,28">

TextBlock x:Name="ApplicationTitle" Text="Persistent Count"

Style="{StaticResourcePhoneTextNormalStyle}"/>

TextBlock x:Name="PageTitle" Text="Tap to Count" Margin="9,-7,0,0"

Style="{StaticResource PhoneTextTitle1Style}"/>

</StackPanel

<!-- Row 1: The text block containing the count -->

TextBlock x:Name="CountTextBlock"Grid.Row="1"TextAlignment="Center" Text="0"/>

<!-- Row 2: The reset button -->

Button x:Name="ResetButton"Grid.Row="2" Click="ResetButton_Click" Content="reset"/>

</Grid

Required Library Class:

Before you proceed to program the events, you need to place the following class in a shared folder. This is the class that will help you save the value of your count in isolated storage:

usingSystem.IO.IsolatedStorage;

namespaceWindowsPhoneApp

{

// Encapsulates a key/value pair stored in Isolated Storage ApplicationSettings

publicclassSetting<T>

{

string name;

T value;

T defaultValue;

boolhasValue;

public Setting(string name, T defaultValue)

{

this.name = name;

this.defaultValue = defaultValue;

}

public T Value

{

get

{

// Check for the cached value

if (!this.hasValue)

{

// Try to get the value from Isolated Storage

if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(

this.name, outthis.value))

{

// It hasn't been set yet

this.value = this.defaultValue;

IsolatedStorageSettings.ApplicationSettings[this.name] =

this.value;

}

this.hasValue = true;

}

returnthis.value;

}

set

{

// Save the value to Isolated Storage

IsolatedStorageSettings.ApplicationSettings[this.name] = value;

this.value = value;

this.hasValue = true;

}

}

public T DefaultValue

{

get { returnthis.defaultValue; }

}

// "Clear" cached value:

publicvoidForceRefresh()

{

this.hasValue = false;

}

}

}

Programming the Events:

To count tap anywhere on the screen:

// Handle a tap anywhere on the page (other than the Button)

protectedoverridevoidOnMouseLeftButtonDown(MouseButtonEventArgs e)

{

base.OnMouseLeftButtonDown(e);

this.count++;

this.CountTextBlock.Text = this.count.ToString("N0");

}

To reset the count press the Reset Button:

privatevoidResetButton_Click(object sender, RoutedEventArgs e)

{

this.count = 0;

this.CountTextBlock.Text = this.count.ToString("N0");

}

If you navigate away from the application, the count will be saved in isolated storage:

protectedoverridevoidOnNavigatedFrom(NavigationEventArgs e)

{

base.OnNavigatedFrom(e);

// Persist state when leaving for any reason (Deactivated or Closing):

this.savedCount.Value = this.count;

}

If you navigate back to the application, the latest value of the count will be loaded from isolated storage:

protectedoverridevoidOnNavigatedFrom(NavigationEventArgs e)

{

base.OnNavigatedFrom(e);

// Persist state when leaving for any reason (Deactivated or Closing):

this.savedCount.Value = this.count;

}

1