Adam Benoit

Windows Phone Development

Age Tracker: Source Code

Posted on

by Adam

Let’s start with the MainPage.xaml file:

<phone:PhoneApplicationPage xmlns:my="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"  
    x:Class="AgeTracker.WP.Views.MainPage"
    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:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
    shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel>
            <controls:Pivot Name="MainPagePivot" Title="AGE TRACKER">
                <controls:PivotItem Name="Person1PivotItem">
                    <!--TitlePanel contains the name of the application and page title-->
                    <StackPanel Orientation="Vertical">
                        <TextBlock Height="41" 
                                   HorizontalAlignment="Center" 
                                   TextAlignment="Center" 
                                   Margin="0,00,0,10" 
                                   Name="person1BirthDateTextBlock" 
                                   Text="" 
                                   Width="400" 
                                   Foreground="{StaticResource PhoneAccentBrush}"
                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                        <StackPanel Orientation="Horizontal">
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="Elapsed" FontSize="{StaticResource PhoneFontSizeLarge}" Foreground="{StaticResource PhoneAccentBrush}"/>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1ElapsedYearsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Years" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1ElapsedMonthsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Months" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1ElapsedDaysTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Days" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1ElapsedHoursTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Hours" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1ElapsedMinutesTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Minutes" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1ElapsedSecondsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Seconds" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                            </StackPanel>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="Total" FontSize="{StaticResource PhoneFontSizeLarge}"  Foreground="{StaticResource PhoneAccentBrush}"/>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1YearsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Years" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1MonthsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Months" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1DaysTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Days" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1HoursTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Hours" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1MinutesTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Minutes" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person1SecondsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Seconds" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                </controls:PivotItem>
                <controls:PivotItem Name="Person2PivotItem">
                    <!--TitlePanel contains the name of the application and page title-->
                    <StackPanel Orientation="Vertical">
                        <TextBlock Height="41" 
                                   HorizontalAlignment="Center" 
                                   TextAlignment="Center" 
                                   Margin="0,00,0,10" 
                                   Name="person2BirthDateTextBlock" 
                                   Text="" 
                                   Width="400" 
                                   Foreground="{StaticResource PhoneAccentBrush}"
                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                        <StackPanel Orientation="Horizontal" Name="Person2Stackpanel" Visibility="Collapsed">
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="Elapsed" FontSize="{StaticResource PhoneFontSizeLarge}" Foreground="{StaticResource PhoneAccentBrush}"/>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2ElapsedYearsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Years" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2ElapsedMonthsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Months" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2ElapsedDaysTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Days" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2ElapsedHoursTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Hours" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2ElapsedMinutesTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Minutes" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2ElapsedSecondsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Seconds" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                            </StackPanel>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="Total" FontSize="{StaticResource PhoneFontSizeLarge}"  Foreground="{StaticResource PhoneAccentBrush}"/>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2YearsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Years" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2MonthsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Months" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2DaysTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Days" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2HoursTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Hours" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2MinutesTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Minutes" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person2SecondsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Seconds" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                        <TextBlock Height="41" 
                                   HorizontalAlignment="Left" 
                                   Margin="6,26,0,0" 
                                   Name="textBlock2" 
                                   Text="Please enable this person in the settings." 
                                   VerticalAlignment="Top" 
                                   Width="446" 
                                   FontSize="24"
                                   TextAlignment="Center"
                                   TextWrapping="Wrap" />
                    </StackPanel>
                </controls:PivotItem>         
                <controls:PivotItem Name="Person3PivotItem">
                    <!--TitlePanel contains the name of the application and page title-->
                    <StackPanel Orientation="Vertical">
                        <TextBlock Height="41" 
                                   HorizontalAlignment="Center" 
                                   TextAlignment="Center" 
                                   Margin="0,00,0,10" 
                                   Name="person3BirthDateTextBlock" 
                                   Text="" 
                                   Width="400" 
                                   Foreground="{StaticResource PhoneAccentBrush}"
                                   FontSize="{StaticResource PhoneFontSizeMedium}" />
                        <StackPanel Orientation="Horizontal" Name="Person3Stackpanel" Visibility="Collapsed">
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="Elapsed" FontSize="{StaticResource PhoneFontSizeLarge}" Foreground="{StaticResource PhoneAccentBrush}"/>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3ElapsedYearsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Years" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3ElapsedMonthsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Months" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3ElapsedDaysTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Days" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3ElapsedHoursTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Hours" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3ElapsedMinutesTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Minutes" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3ElapsedSecondsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Seconds" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                            </StackPanel>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="Total" FontSize="{StaticResource PhoneFontSizeLarge}"  Foreground="{StaticResource PhoneAccentBrush}"/>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3YearsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Years" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3MonthsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Months" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3DaysTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Days" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3HoursTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Hours" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3MinutesTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Minutes" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Name="Person3SecondsTextBlock" Text="0" Padding="20,10,5,10" FontSize="28" />
                                    <TextBlock Text="Seconds" Padding="5,10,5,10" FontSize="28" />
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                        <TextBlock Height="41" 
                                   HorizontalAlignment="Left" 
                                   Margin="6,26,0,0" 
                                   Name="textBlock3" 
                                   Text="Please enable this person in the settings." 
                                   VerticalAlignment="Top" 
                                   Width="446" 
                                   FontSize="24"
                                   TextAlignment="Center"
                                   TextWrapping="Wrap" />
                    </StackPanel>
                </controls:PivotItem>
            </controls:Pivot>
            <my:AdControl Name="Ad1" Height="80" Width="480" Margin="0,50,0,0" />
        </StackPanel>

    </Grid>
</phone:PhoneApplicationPage>

And its corresponding MainPage.xaml.cs:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Threading;
using Itenso.TimePeriod;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;

namespace AgeTracker.WP.Views
{
    public partial class MainPage : PhoneApplicationPage
    {
        private readonly AppSettings _appSettings = new AppSettings();

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Add an Application Bar with a 'setting menu item.
            ApplicationBar = new ApplicationBar();
            ApplicationBar.IsMenuEnabled = true;
            ApplicationBar.IsVisible = true;
            ApplicationBar.Opacity = 1.0;
            ApplicationBar.Mode = ApplicationBarMode.Minimized;

            var settingsItem =
                new ApplicationBarIconButton(new Uri("/Images/appbar.feature.settings.rest.png", UriKind.Relative));
            var helpItem =
                new ApplicationBarIconButton(new Uri("/Images/appbar.questionmark.rest.png", UriKind.Relative));
            var shareItem = new ApplicationBarIconButton(new Uri("/Images/share.png", UriKind.Relative));
            var rateItem = new ApplicationBarIconButton(new Uri("/Images/appbar.favs.rest.png", UriKind.Relative));

            settingsItem.Text = "settings";
            helpItem.Text = "help";
            shareItem.Text = "share";
            rateItem.Text = "rate";

            settingsItem.Click += SettingsClick;
            helpItem.Click += HelpClick;
            shareItem.Click += TwitterItemOnClick;
            rateItem.Click += RateItemOnClick;

            ApplicationBar.Buttons.Add(shareItem);
            ApplicationBar.Buttons.Add(settingsItem);
            ApplicationBar.Buttons.Add(helpItem);
            ApplicationBar.Buttons.Add(rateItem);

            Ad1.ApplicationId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
            Ad1.AdUnitId = "xxxxxxxx";
        }

        private void TwitterItemOnClick(object sender, EventArgs eventArgs)
        {
            try
            {
                ShareStatusTask shareStatusTask = new ShareStatusTask();
                string message = string.Empty;
                string name = string.Empty;
                if (MainPagePivot.SelectedIndex == 0)
                {
                    message = CalculateSinceBirth(_appSettings.Birthday1DateSetting,
                                                  _appSettings.Birthday1TimeSetting,
                                                  _appSettings.UseTime1CheckBoxSetting).GetDescription();
                    name = _appSettings.Birthday1NameSetting;
                }
                else if (MainPagePivot.SelectedIndex == 1)
                {
                    if (_appSettings.UsePerson2CheckBoxSetting)
                    {
                        message = CalculateSinceBirth(_appSettings.Birthday2DateSetting,
                                                      _appSettings.Birthday2TimeSetting,
                                                      _appSettings.UseTime2CheckBoxSetting).GetDescription();
                        name = _appSettings.Birthday2NameSetting;
                    }
                    else
                    {
                        MessageBox.Show("Please enable this person before trying to share their age.", "Not Enabled.",
                                        MessageBoxButton.OK);
                        return;
                    }
                }
                else if (MainPagePivot.SelectedIndex == 2)
                {
                    if (_appSettings.UsePerson3CheckBoxSetting)
                    {
                        message = CalculateSinceBirth(_appSettings.Birthday3DateSetting,
                                                      _appSettings.Birthday3TimeSetting,
                                                      _appSettings.UseTime3CheckBoxSetting).GetDescription();
                        name = _appSettings.Birthday3NameSetting;
                    }
                    else
                    {
                        MessageBox.Show("Please enable this person before trying to share their age.", "Not Enabled.",
                                        MessageBoxButton.OK);
                        return;
                    }
                }
                shareStatusTask.Status = name + " is " + message +
                                         " old right now. \n\nSent by the Age Tracker app for Windows Phone.";
                shareStatusTask.Show();
            }
            catch (InvalidOperationException ex)
            {

                //throw;
            }
        }

        private void HelpClick(object sender, EventArgs eventArgs)
        {
            NavigationService.Navigate(new Uri("/Views/Help.xaml", UriKind.Relative));
        }

        private void RateItemOnClick(object sender, EventArgs eventArgs)
        {
            MarketplaceReviewTask rate = new MarketplaceReviewTask();
            rate.Show();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Person1PivotItem.Header = _appSettings.Birthday1NameSetting;
            if (_appSettings.UseTime1CheckBoxSetting)
            {
                person1BirthDateTextBlock.Text =
                    _appSettings.Birthday1DateSetting.ToString("dddd, MMMM dd yyyy", CultureInfo.CurrentCulture) +
                    _appSettings.Birthday1TimeSetting.ToString(" hh:mm tt", CultureInfo.CurrentCulture);
            }
            else
            {
                person1BirthDateTextBlock.Text = _appSettings.Birthday1DateSetting.ToString("dddd, MMMM dd yyyy",
                                                                                            CultureInfo.CurrentCulture);
            }

            if (_appSettings.UsePerson2CheckBoxSetting)
            {
                Person2PivotItem.Header = _appSettings.Birthday2NameSetting;
                if (_appSettings.UseTime2CheckBoxSetting)
                {
                    person2BirthDateTextBlock.Text =
                        _appSettings.Birthday2DateSetting.ToString("dddd, MMMM dd yyyy", CultureInfo.CurrentCulture) +
                        _appSettings.Birthday2TimeSetting.ToString(" hh:mm tt", CultureInfo.CurrentCulture);
                }
                else
                {
                    person2BirthDateTextBlock.Text = _appSettings.Birthday2DateSetting.ToString("dddd, MMMM dd yyyy",
                                                                                                CultureInfo.
                                                                                                    CurrentCulture);
                }
                SetUIElementVisibility(person2BirthDateTextBlock, true);
                SetUIElementVisibility(textBlock2, false);
                SetUIElementVisibility(Person2Stackpanel, true);
            }
            else
            {
                Person2PivotItem.Header = "Person 2";
                SetUIElementVisibility(person2BirthDateTextBlock, false);
                SetUIElementVisibility(textBlock2, true);
                SetUIElementVisibility(Person2Stackpanel, false);
            }

            if (_appSettings.UsePerson3CheckBoxSetting)
            {
                Person3PivotItem.Header = _appSettings.Birthday3NameSetting;
                if (_appSettings.UseTime3CheckBoxSetting)
                {
                    person3BirthDateTextBlock.Text =
                        _appSettings.Birthday3DateSetting.ToString("dddd, MMMM dd yyyy", CultureInfo.CurrentCulture) +
                        _appSettings.Birthday3TimeSetting.ToString(" hh:mm tt", CultureInfo.CurrentCulture);
                }
                else
                {
                    person3BirthDateTextBlock.Text = _appSettings.Birthday3DateSetting.ToString("dddd, MMMM dd yyyy",
                                                                                                CultureInfo.
                                                                                                    CurrentCulture);
                }
                SetUIElementVisibility(person3BirthDateTextBlock, true);
                SetUIElementVisibility(textBlock3, false);
                SetUIElementVisibility(Person3Stackpanel, true);
            }
            else
            {
                Person3PivotItem.Header = "Person 3";
                SetUIElementVisibility(person3BirthDateTextBlock, true);
                SetUIElementVisibility(textBlock3, true);
                SetUIElementVisibility(Person3Stackpanel, false);
            }

            var tmr = new DispatcherTimer();
            tmr.Interval = TimeSpan.FromSeconds(1);
            tmr.Tick += OnTimerTick;
            tmr.Start();
        }

        private void OnTimerTick(object sender, EventArgs e)
        {
            // textBlock1.Text = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
            UpdatePerson1UI(CalculateSinceBirth(_appSettings.Birthday1DateSetting, _appSettings.Birthday1TimeSetting,
                                                _appSettings.UseTime1CheckBoxSetting));
            UpdatePerson2UI(CalculateSinceBirth(_appSettings.Birthday2DateSetting, _appSettings.Birthday2TimeSetting,
                                                _appSettings.UseTime2CheckBoxSetting));
            UpdatePerson3UI(CalculateSinceBirth(_appSettings.Birthday3DateSetting, _appSettings.Birthday3TimeSetting,
                                                _appSettings.UseTime3CheckBoxSetting));
        }

        private DateDiff CalculateSinceBirth(DateTime Date, DateTime Time, bool UseTime)
        {
            DateTime Birthday;
            if (UseTime)
            {
                Birthday = new DateTime(Date.Year, Date.Month, Date.Day, Time.Hour, Time.Minute, 0);
            }
            else
            {
                Birthday = new DateTime(Date.Year, Date.Month, Date.Day, 0, 0, 0);
            }

            return new DateDiff(Birthday, DateTime.Now);
        }

        private void UpdatePerson1UI(DateDiff diff)
        {
            Person1YearsTextBlock.Text = diff.Years.ToString(CultureInfo.InvariantCulture);
            Person1MonthsTextBlock.Text = diff.Months.ToString(CultureInfo.InvariantCulture);
            Person1DaysTextBlock.Text = diff.Days.ToString(CultureInfo.InvariantCulture);
            Person1HoursTextBlock.Text = diff.Hours.ToString(CultureInfo.InvariantCulture);
            Person1MinutesTextBlock.Text = diff.Minutes.ToString(CultureInfo.InvariantCulture);
            Person1SecondsTextBlock.Text = diff.Seconds.ToString(CultureInfo.InvariantCulture);

            Person1ElapsedYearsTextBlock.Text = diff.ElapsedYears.ToString(CultureInfo.InvariantCulture);
            Person1ElapsedMonthsTextBlock.Text = diff.ElapsedMonths.ToString(CultureInfo.InvariantCulture);
            Person1ElapsedDaysTextBlock.Text = diff.ElapsedDays.ToString(CultureInfo.InvariantCulture);
            Person1ElapsedHoursTextBlock.Text = diff.ElapsedHours.ToString(CultureInfo.InvariantCulture);
            Person1ElapsedMinutesTextBlock.Text = diff.ElapsedMinutes.ToString(CultureInfo.InvariantCulture);
            Person1ElapsedSecondsTextBlock.Text = diff.ElapsedSeconds.ToString(CultureInfo.InvariantCulture);
        }

        private void UpdatePerson2UI(DateDiff diff)
        {
            Person2YearsTextBlock.Text = diff.Years.ToString(CultureInfo.InvariantCulture);
            Person2MonthsTextBlock.Text = diff.Months.ToString(CultureInfo.InvariantCulture);
            Person2DaysTextBlock.Text = diff.Days.ToString(CultureInfo.InvariantCulture);
            Person2HoursTextBlock.Text = diff.Hours.ToString(CultureInfo.InvariantCulture);
            Person2MinutesTextBlock.Text = diff.Minutes.ToString(CultureInfo.InvariantCulture);
            Person2SecondsTextBlock.Text = diff.Seconds.ToString(CultureInfo.InvariantCulture);

            Person2ElapsedYearsTextBlock.Text = diff.ElapsedYears.ToString(CultureInfo.InvariantCulture);
            Person2ElapsedMonthsTextBlock.Text = diff.ElapsedMonths.ToString(CultureInfo.InvariantCulture);
            Person2ElapsedDaysTextBlock.Text = diff.ElapsedDays.ToString(CultureInfo.InvariantCulture);
            Person2ElapsedHoursTextBlock.Text = diff.ElapsedHours.ToString(CultureInfo.InvariantCulture);
            Person2ElapsedMinutesTextBlock.Text = diff.ElapsedMinutes.ToString(CultureInfo.InvariantCulture);
            Person2ElapsedSecondsTextBlock.Text = diff.ElapsedSeconds.ToString(CultureInfo.InvariantCulture);
        }

        private void UpdatePerson3UI(DateDiff diff)
        {
            Person3YearsTextBlock.Text = diff.Years.ToString(CultureInfo.InvariantCulture);
            Person3MonthsTextBlock.Text = diff.Months.ToString(CultureInfo.InvariantCulture);
            Person3DaysTextBlock.Text = diff.Days.ToString(CultureInfo.InvariantCulture);
            Person3HoursTextBlock.Text = diff.Hours.ToString(CultureInfo.InvariantCulture);
            Person3MinutesTextBlock.Text = diff.Minutes.ToString(CultureInfo.InvariantCulture);
            Person3SecondsTextBlock.Text = diff.Seconds.ToString(CultureInfo.InvariantCulture);

            Person3ElapsedYearsTextBlock.Text = diff.ElapsedYears.ToString(CultureInfo.InvariantCulture);
            Person3ElapsedMonthsTextBlock.Text = diff.ElapsedMonths.ToString(CultureInfo.InvariantCulture);
            Person3ElapsedDaysTextBlock.Text = diff.ElapsedDays.ToString(CultureInfo.InvariantCulture);
            Person3ElapsedHoursTextBlock.Text = diff.ElapsedHours.ToString(CultureInfo.InvariantCulture);
            Person3ElapsedMinutesTextBlock.Text = diff.ElapsedMinutes.ToString(CultureInfo.InvariantCulture);
            Person3ElapsedSecondsTextBlock.Text = diff.ElapsedSeconds.ToString(CultureInfo.InvariantCulture);
        }

        private void SetUIElementVisibility(UIElement control, bool visible)
        {
            if (visible)
            {
                control.Visibility = Visibility.Visible;
            }
            else
            {
                control.Visibility = Visibility.Collapsed;
            }
        }

        private void SettingsClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Settings.xaml", UriKind.Relative));
        }
    }
}

About Adam

Adam Benoit is a Quality Assurance Analyst, Contributing Author and Windows Phone Developer. View all posts by Adam →
This entry was posted in Applications, Development, Featured, Howto. Bookmark the permalink.

Comments are closed.