Age Tracker: Source Code
July 7, 2012Next up is the Settings.xaml file:
<phone:PhoneApplicationPage x:Class="AgeTracker.WP.Views.Settings" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:telerikInput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input" xmlns:local="clr-namespace:AgeTracker.WP" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="False" Loaded="PhoneApplicationPage_Loaded"> <phone:PhoneApplicationPage.Resources> <local:AppSettings x:Key="appSettings"></local:AppSettings> </phone:PhoneApplicationPage.Resources> <!--LayoutRoot contains the root grid where all other page content is placed--> <Grid x:Name="LayoutRoot"> <controls:Pivot Title="AGE TRACKER"> <!--Panorama item one--> <controls:PivotItem Header="Person 1"> <Grid> <StackPanel> <TextBlock Text="Name" Name="person1NameLabelTextBlock" FontSize="24" Foreground="{StaticResource PhoneAccentBrush}" /> <TextBox Name="person1NameTextBox" Text="{Binding Source={StaticResource appSettings}, Path=Birthday1NameSetting, Mode=TwoWay}" /> <TextBlock Text="Birthdate" Name="Person1BirthdateLabelTextBlock" FontSize="24" Foreground="{StaticResource PhoneAccentBrush}" /> <StackPanel Orientation="Horizontal"> <telerikInput:RadDatePicker x:Name="Birthday1RadDatePicker" Width="275" Value="{Binding Source={StaticResource appSettings}, Path=Birthday1DateSetting, Mode=TwoWay}" /> <CheckBox Content="Use Time " Height="Auto" HorizontalAlignment="Left" Margin="10,0,0,0" Name="UseTime1CheckBox" VerticalAlignment="Top" IsChecked="{Binding Source={StaticResource appSettings}, Path=UseTime1CheckBoxSetting, Mode=TwoWay}" Checked="UseTime1CheckBoxChecked" Unchecked="UseTime1CheckBoxUnchecked" /> </StackPanel> <telerikInput:RadTimePicker x:Name="Birthday1RadTimePicker" Value="{Binding Source={StaticResource appSettings}, Path=Birthday1TimeSetting, Mode=TwoWay}" /> </StackPanel> </Grid> </controls:PivotItem> <!--PivotItem item two--> <controls:PivotItem Header="Person 2"> <Grid> <StackPanel> <CheckBox Content="Enable This Person " Height="Auto" HorizontalAlignment="Left" Margin="10,0,0,0" Name="UsePerson2CheckBox" VerticalAlignment="Top" IsChecked="{Binding Source={StaticResource appSettings}, Path=UsePerson2CheckBoxSetting, Mode=TwoWay}" Checked="UsePerson2CheckBoxChecked" Unchecked="UsePerson2CheckBoxUnchecked" /> <StackPanel Name="Person2StackPanel"> <TextBlock Text="Name" Name="person2NameLabelTextBlock" FontSize="24" Foreground="{StaticResource PhoneAccentBrush}" /> <TextBox Name="person2NameTextBox" Text="{Binding Source={StaticResource appSettings}, Path=Birthday2NameSetting, Mode=TwoWay}" /> <TextBlock Text="Birthdate" Name="Person2BirthdateLabelTextBlock" FontSize="24" Foreground="{StaticResource PhoneAccentBrush}" /> <StackPanel Orientation="Horizontal"> <telerikInput:RadDatePicker x:Name="Birthday2RadDatePicker" Width="275" Value="{Binding Source={StaticResource appSettings}, Path=Birthday2DateSetting, Mode=TwoWay}" /> <CheckBox Content="Use Time " Height="Auto" HorizontalAlignment="Left" Margin="10,0,0,0" Name="UseTime2CheckBox" VerticalAlignment="Top" IsChecked="{Binding Source={StaticResource appSettings}, Path=UseTime2CheckBoxSetting, Mode=TwoWay}" Unchecked="UseTime2CheckBoxUnchecked" Checked="UseTime2CheckBoxChecked" /> </StackPanel> <telerikInput:RadTimePicker x:Name="Birthday2RadTimePicker" Value="{Binding Source={StaticResource appSettings}, Path=Birthday2TimeSetting, Mode=TwoWay}" /> </StackPanel> </StackPanel> </Grid> </controls:PivotItem> <!--PivotItem item three--> <controls:PivotItem Header="Person 3"> <Grid> <StackPanel> <CheckBox Content="Enable This Person " Height="Auto" HorizontalAlignment="Left" Margin="10,0,0,0" Name="UsePerson3CheckBox" VerticalAlignment="Top" IsChecked="{Binding Source={StaticResource appSettings}, Path=UsePerson3CheckBoxSetting, Mode=TwoWay}" Checked="UsePerson3CheckBoxChecked" Unchecked="UsePerson3CheckBoxUnchecked" /> <StackPanel Name="Person3StackPanel"> <TextBlock Text="Name" Name="person3NameLabelTextBlock" FontSize="24" Foreground="{StaticResource PhoneAccentBrush}" /> <TextBox Name="person3NameTextBox" Text="{Binding Source={StaticResource appSettings}, Path=Birthday3NameSetting, Mode=TwoWay}" /> <TextBlock Text="Birthdate" Name="Person3BirthdateLabelTextBlock" FontSize="24" Foreground="{StaticResource PhoneAccentBrush}" /> <StackPanel Orientation="Horizontal"> <telerikInput:RadDatePicker x:Name="Birthday3RadDatePicker" Width="275" Value="{Binding Source={StaticResource appSettings}, Path=Birthday3DateSetting, Mode=TwoWay}" /> <CheckBox Content="Use Time " Height="Auto" HorizontalAlignment="Left" Margin="10,0,0,0" Name="UseTime3CheckBox" VerticalAlignment="Top" IsChecked="{Binding Source={StaticResource appSettings}, Path=UseTime3CheckBoxSetting, Mode=TwoWay}" Checked="UseTime3CheckBoxChecked" Unchecked="UseTime3CheckBoxUnchecked" /> </StackPanel> <telerikInput:RadTimePicker x:Name="Birthday3RadTimePicker" Value="{Binding Source={StaticResource appSettings}, Path=Birthday3TimeSetting, Mode=TwoWay}" /> </StackPanel> </StackPanel> </Grid> </controls:PivotItem> </controls:Pivot> </Grid> <!--Panorama-based applications should not show an ApplicationBar--> </phone:PhoneApplicationPage>
And its code behind Settings.xaml.cs file:
using System; using System.Windows; using Microsoft.Phone.Controls; namespace AgeTracker.WP.Views { public partial class Settings : PhoneApplicationPage { bool _loaded; AppSettings _appSettings = new AppSettings(); public Settings() { InitializeComponent(); } private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { Birthday1RadDatePicker.MaxValue = DateTime.Now; Birthday1RadDatePicker.CancelButtonIconUri = new Uri("/Images/appbar.cancel.rest.png", UriKind.Relative); Birthday1RadDatePicker.OkButtonIconUri = new Uri("/Images/appbar.check.rest.png", UriKind.Relative); Birthday1RadTimePicker.CancelButtonIconUri = new Uri("/Images/appbar.cancel.rest.png", UriKind.Relative); Birthday1RadTimePicker.OkButtonIconUri = new Uri("/Images/appbar.check.rest.png", UriKind.Relative); Birthday2RadDatePicker.MaxValue = DateTime.Now; Birthday2RadDatePicker.CancelButtonIconUri = new Uri("/Images/appbar.cancel.rest.png", UriKind.Relative); Birthday2RadDatePicker.OkButtonIconUri = new Uri("/Images/appbar.check.rest.png", UriKind.Relative); Birthday2RadTimePicker.CancelButtonIconUri = new Uri("/Images/appbar.cancel.rest.png", UriKind.Relative); Birthday2RadTimePicker.OkButtonIconUri = new Uri("/Images/appbar.check.rest.png", UriKind.Relative); Birthday3RadDatePicker.MaxValue = DateTime.Now; Birthday3RadDatePicker.CancelButtonIconUri = new Uri("/Images/appbar.cancel.rest.png", UriKind.Relative); Birthday3RadDatePicker.OkButtonIconUri = new Uri("/Images/appbar.check.rest.png", UriKind.Relative); Birthday3RadTimePicker.CancelButtonIconUri = new Uri("/Images/appbar.cancel.rest.png", UriKind.Relative); Birthday3RadTimePicker.OkButtonIconUri = new Uri("/Images/appbar.check.rest.png", UriKind.Relative); _loaded = true; SetUIElementVisibility(Person2StackPanel, _appSettings.UsePerson2CheckBoxSetting); SetUIElementVisibility(Person3StackPanel, _appSettings.UsePerson3CheckBoxSetting); SetUIElementVisibility(Birthday1RadTimePicker, _appSettings.UseTime1CheckBoxSetting); SetUIElementVisibility(Birthday2RadTimePicker, _appSettings.UseTime2CheckBoxSetting); SetUIElementVisibility(Birthday3RadTimePicker, _appSettings.UseTime3CheckBoxSetting); } private void SetUIElementVisibility(UIElement control, bool visible) { if (_loaded) { if (visible) { control.Visibility = Visibility.Visible; } else { control.Visibility = Visibility.Collapsed; } } } private void UsePerson2CheckBoxChecked(object sender, RoutedEventArgs e) { SetUIElementVisibility(Person2StackPanel, true); } private void UsePerson2CheckBoxUnchecked(object sender, RoutedEventArgs e) { SetUIElementVisibility(Person2StackPanel, false); } private void UsePerson3CheckBoxChecked(object sender, RoutedEventArgs e) { SetUIElementVisibility(Person3StackPanel, true); } private void UsePerson3CheckBoxUnchecked(object sender, RoutedEventArgs e) { SetUIElementVisibility(Person3StackPanel, false); } private void UseTime1CheckBoxChecked(object sender, RoutedEventArgs e) { SetUIElementVisibility(Birthday1RadTimePicker, true); } private void UseTime1CheckBoxUnchecked(object sender, RoutedEventArgs e) { SetUIElementVisibility(Birthday1RadTimePicker, false); } private void UseTime2CheckBoxChecked(object sender, RoutedEventArgs e) { SetUIElementVisibility(Birthday2RadTimePicker, true); } private void UseTime2CheckBoxUnchecked(object sender, RoutedEventArgs e) { SetUIElementVisibility(Birthday2RadTimePicker, false); } private void UseTime3CheckBoxChecked(object sender, RoutedEventArgs e) { SetUIElementVisibility(Birthday3RadTimePicker, true); } private void UseTime3CheckBoxUnchecked(object sender, RoutedEventArgs e) { SetUIElementVisibility(Birthday3RadTimePicker, false); } } }
And saving settings is driven by AppSettings.cs file:
using System; using System.IO.IsolatedStorage; using System.Diagnostics; namespace AgeTracker.WP { public class AppSettings { // Our isolated storage settings IsolatedStorageSettings isolatedStore; #region Person 1 Settings const string Birthday1DateSettingKeyName = "Birthday1DateSetting"; const string Birthday1NameSettingKeyName = "Birthday1NameSetting"; const string UseTime1CheckBoxSettingKeyName = "UseTime1CheckBoxSetting"; const string Birthday1TimeSettingKeyName = "Birthday1TimeSetting"; DateTime Birthday1DateSettingDefault = new DateTime(1970, 1, 1, 0, 0, 0); const string Birthday1NameSettingDefault = "Person 1"; const bool UseTime1CheckBoxSettingDefault = true; DateTime Birthday1TimeSettingDefault = new DateTime(1970, 1, 1, 0, 0, 0); public string Birthday1NameSetting { get { return GetValueOrDefault<string>(Birthday1NameSettingKeyName, Birthday1NameSettingDefault); } set { AddOrUpdateValue(Birthday1NameSettingKeyName, value); Save(); } } public DateTime Birthday1DateSetting { get { return GetValueOrDefault<DateTime>(Birthday1DateSettingKeyName, Birthday1DateSettingDefault); } set { AddOrUpdateValue(Birthday1DateSettingKeyName, value); Save(); } } public bool UseTime1CheckBoxSetting { get { return GetValueOrDefault<bool>(UseTime1CheckBoxSettingKeyName, UseTime1CheckBoxSettingDefault); } set { AddOrUpdateValue(UseTime1CheckBoxSettingKeyName, value); Save(); } } public DateTime Birthday1TimeSetting { get { return GetValueOrDefault<DateTime>(Birthday1TimeSettingKeyName, Birthday1TimeSettingDefault); } set { AddOrUpdateValue(Birthday1TimeSettingKeyName, value); Save(); } } #endregion #region Person 2 const string UsePerson2CheckBoxSettingKeyName = "UsePerson2CheckBoxSetting"; const string Birthday2DateSettingKeyName = "Birthday2DateSetting"; const string Birthday2NameSettingKeyName = "Birthday2NameSetting"; const string UseTime2CheckBoxSettingKeyName = "UseTime2CheckBoxSetting"; const string Birthday2TimeSettingKeyName = "Birthday2TimeSetting"; const bool UsePerson2CheckBoxSettingDefault = false; DateTime Birthday2DateSettingDefault = new DateTime(1970, 1, 1, 0, 0, 0); const string Birthday2NameSettingDefault = "Person 2"; const bool UseTime2CheckBoxSettingDefault = true; DateTime Birthday2TimeSettingDefault = new DateTime(1970, 1, 1, 0, 0, 0); public bool UsePerson2CheckBoxSetting { get { return GetValueOrDefault<bool>(UsePerson2CheckBoxSettingKeyName, UsePerson2CheckBoxSettingDefault); } set { AddOrUpdateValue(UsePerson2CheckBoxSettingKeyName, value); Save(); } } public string Birthday2NameSetting { get { return GetValueOrDefault<string>(Birthday2NameSettingKeyName, Birthday2NameSettingDefault); } set { AddOrUpdateValue(Birthday2NameSettingKeyName, value); Save(); } } public DateTime Birthday2DateSetting { get { return GetValueOrDefault<DateTime>(Birthday2DateSettingKeyName, Birthday2DateSettingDefault); } set { AddOrUpdateValue(Birthday2DateSettingKeyName, value); Save(); } } public bool UseTime2CheckBoxSetting { get { return GetValueOrDefault<bool>(UseTime2CheckBoxSettingKeyName, UseTime2CheckBoxSettingDefault); } set { AddOrUpdateValue(UseTime2CheckBoxSettingKeyName, value); Save(); } } public DateTime Birthday2TimeSetting { get { return GetValueOrDefault<DateTime>(Birthday2TimeSettingKeyName, Birthday2TimeSettingDefault); } set { AddOrUpdateValue(Birthday2TimeSettingKeyName, value); Save(); } } #endregion #region Person 3 Settings const string UsePerson3CheckBoxSettingKeyName = "UsePerson3CheckBoxSetting"; const string Birthday3DateSettingKeyName = "Birthday3DateSetting"; const string Birthday3NameSettingKeyName = "Birthday3NameSetting"; const string UseTime3CheckBoxSettingKeyName = "UseTime3CheckBoxSetting"; const string Birthday3TimeSettingKeyName = "Birthday3TimeSetting"; const bool UsePerson3CheckBoxSettingDefault = false; DateTime Birthday3DateSettingDefault = new DateTime(1970, 1, 1, 0, 0, 0); const string Birthday3NameSettingDefault = "Person 3"; const bool UseTime3CheckBoxSettingDefault = true; DateTime Birthday3TimeSettingDefault = new DateTime(1970, 1, 1, 0, 0, 0); public bool UsePerson3CheckBoxSetting { get { return GetValueOrDefault<bool>(UsePerson3CheckBoxSettingKeyName, UsePerson3CheckBoxSettingDefault); } set { AddOrUpdateValue(UsePerson3CheckBoxSettingKeyName, value); Save(); } } public string Birthday3NameSetting { get { return GetValueOrDefault<string>(Birthday3NameSettingKeyName, Birthday3NameSettingDefault); } set { AddOrUpdateValue(Birthday3NameSettingKeyName, value); Save(); } } public DateTime Birthday3DateSetting { get { return GetValueOrDefault<DateTime>(Birthday3DateSettingKeyName, Birthday3DateSettingDefault); } set { AddOrUpdateValue(Birthday3DateSettingKeyName, value); Save(); } } public bool UseTime3CheckBoxSetting { get { return GetValueOrDefault<bool>(UseTime3CheckBoxSettingKeyName, UseTime3CheckBoxSettingDefault); } set { AddOrUpdateValue(UseTime3CheckBoxSettingKeyName, value); Save(); } } public DateTime Birthday3TimeSetting { get { return GetValueOrDefault<DateTime>(Birthday3TimeSettingKeyName, Birthday3TimeSettingDefault); } set { AddOrUpdateValue(Birthday3TimeSettingKeyName, value); Save(); } } #endregion public AppSettings() { try { // Get the settings for this application. isolatedStore = IsolatedStorageSettings.ApplicationSettings; } catch (Exception e) { Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString()); } } public bool AddOrUpdateValue(string Key, Object value) { bool valueChanged = false; // If the key exists if (isolatedStore.Contains(Key)) { // If the value has changed if (isolatedStore[Key] != value) { // Store the new value isolatedStore[Key] = value; valueChanged = true; } } // Otherwise create the key. else { isolatedStore.Add(Key, value); valueChanged = true; } return valueChanged; } public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue) { valueType value; // If the key exists, retrieve the value. if (isolatedStore.Contains(Key)) { value = (valueType)isolatedStore[Key]; } // Otherwise, use the default value. else { value = defaultValue; } return value; } public void Save() { isolatedStore.Save(); } } }
Comments are closed.