Developing Windows Phone Applications Part 4
This will be the last post of my journey to learn the basics of Windows Phone Development. There is a lot more to learn about this wonderful platform and I plan to keep posting on the topic In fact, I am working on a new application that I will soon blog about. The theme behind the sample app is “Three Screens and a Cloud”. I want to show the integration of mobile devices with the entire stack. I believe, that mobile applications are just a small part of the overall application so I want to give an example of that.
To wrap up the NerdDinner App, I wanted to add a few things. Add landscape layout support and save data to the isolate storage. After thinking about it for a few minutes, I realized that the NerdDinner app does not really need landscape layout support. There are only a few scenarios where landscape makes senses. Apps requiring a lot of data entry and games are the most popular types that would required landscape layout.
I still wanted to add layout support just to have a reference I can point people to. As I was doing my research, I came across this post which has a great explanation of the different ways we can handle multiple layouts. The way I am handling layout is to not support it :-). I am adding the following code the every page:
SupportedOrientations = SupportedPageOrientation.Portrait;
This will ensure that when the device is rotated, the UI will not respond to the rotation. So the screen will look like this:
This allows to not have to worry how it will look in landscape more because we all letting the user know that it is not supported. We can always allow it to render in landscape more which without any modifications will look like this:
So to learn about the different strategies you can apply to “truly” support portrait and landscape layouts check out this post.
Saving Data
In a nutshell, “Isolated storage enables managed applications to create and maintain local storage. The mobile architecture is similar to the Silverlight-based applications on Windows. All I/O operations are restricted to isolated storage and do not have direct access to the underlying operating system file system. Ultimately, this helps to provide security and prevents unauthorized access and data corruption.”
We are free to save the applications settings anywhere in isolated storage but there is a especial api for application settings. We are going to use this api. First we need to collect the user settings. To do this, we are adding an page to our app. The XAML looks like this:
<navigation:PhoneApplicationPage x:Class="NerdDinnerPhoneApp.SettingsPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls" xmlns:navigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" SupportedOrientations="Portrait" mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="480"> <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}"> <Grid.RowDefinitions> <RowDefinition Height="170"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--This is the name of the application and page title--> <Grid Grid.Row="0" x:Name="TitleGrid"> <TextBlock x:Name="ApplicationName" Text="SETTINGS" /> <TextBlock Text="Nerd Dinner App" /> </Grid> <!--This section is empty. Place new content here Grid.Row="1"--> <ScrollViewer BorderThickness="0" Grid.Row="1"> <StackPanel> <TextBlock Text="Save Last Results" Style="{StaticResource PhoneTextBodyTextStyle}"></TextBlock> <c:ToggleControlSwitch x:Name="SaveResults" HorizontalAlignment="Left" Height="60" Width="120" /> <TextBlock Text="Default City" Style="{StaticResource PhoneTextBodyTextStyle}"></TextBlock> <TextBox x:Name="DefaultCity" Text="Philadelphia" ></TextBox> </StackPanel> </ScrollViewer> </Grid> </navigation:PhoneApplicationPage>We are collecting two pieces of information. We want to know if the user wants to save the last search (based on this location) . We also want to get the default city in case the Geo coordinates cant be acquired. I am using a toggle control switch just because we can :-).
Now that form is created, we need a way to get and set values from isolated storage. To help with this task I created this helper class: (that class should be refactor to allow generic storage as well but for now lets leave it the way it is)
public class StorageHelper { IsolatedStorageSettings _isolatedstore; public StorageHelper() { try { _isolatedstore = IsolatedStorageSettings.ApplicationSettings; } catch (Exception e) { MessageBox.Show("Exception while using IsolatedStorageSettings: " + e.ToString()); } } public void AddOrUpdateValue( string Key, Object value) { if (! _isolatedstore.Contains(Key)) _isolatedstore.Add(Key, value); _isolatedstore[Key] = value; } public T GetValue<T>(string Key) { T value; try { value = (T)_isolatedstore[Key]; } catch (Exception) { value = default(T); } return value; } public void Save() { _isolatedstore.Save(); } } }
From the code behind of the Setting Page we have:
StorageHelper _s;
public SettingsPage()
{
InitializeComponent();
_s = new StorageHelper();
string defaultCity = _s.GetValue<string>(DefaultCity.Name);
if (defaultCity == null)
defaultCity = "Philadelphia";
DefaultCity.Text = defaultCity;
}
protected override void OnNavigatedFrom(Microsoft.Phone.Navigation.PhoneNavigationEventArgs e)
{
_s.AddOrUpdateValue(DefaultCity.Name, DefaultCity.Text);
_s.Save();
base.OnNavigatedFrom(e);
}
And that is it. We now have a way to save settings to isolated storage.
My name is Dani Diaz. I work for Microsoft as a Developer Evangelist covering the Northern East Coast. Oh and before I forget. The opinions express on this site are my owns and not that of my employer.



