Every time I create a new app, I have to spend at least 10-15 minutes just to remove all the clutter that the guys who created them put in. Probably they thought than an average developer won’t be smart enough to figure out that THIS IS APPLICATION TITLE, DUDE!!1 And why do you need a backing field for each and every control on the page? Arrgh.

My favorite is that vital ‘phoneApplicationInitialized‘ variable. Just, look at this code (I’ve changed the order of class members and removed some unrelated calls, but that doesn’t matter):

private bool phoneApplicationInitialized = false;
 
public App()
{
    InitializeComponent();
    InitializePhoneApplication();
}
 
private void InitializePhoneApplication()
{
    if (phoneApplicationInitialized)
        return;
 
    RootFrame = new PhoneApplicationFrame();
    RootFrame.Navigated += CompleteInitializePhoneApplication;
    RootFrame.NavigationFailed += OnNavigationFailed;
 
    // Ensure we don't initialize again
    phoneApplicationInitialized = true;
}

So its value is modified only inside InitializePhoneApplication method that is called only from the class constructor. That first condition check never returns and there is no way to ‘initialize again’.

Anyway, I decided to create my own reusable page and application definition templates to save some time. Obviously, they may not suit your needs, so think about them as a reference or an alternative to the predefined templates.

App template

<Application
    x:Class="$namespace$.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

    <Application.Resources>
        <system:String x:Key="ApplicationTitle">APPLICATION TITLE</system:String>
        
        <!-- Page margins -->
        <Thickness x:Key="PageHeaderPanelMargin">12,17,0,28</Thickness>
        <Thickness x:Key="PageNameLabelMargin">9,-7,0,0</Thickness>
        <Thickness x:Key="PageContentPanelMargin">12,0</Thickness>
        
        <Style x:Key="DefaultPageStyle" TargetType="phone:PhoneApplicationPage">
            <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>
            <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}"/>
            <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
            <Setter Property="SupportedOrientations" Value="Portrait"/>
            <Setter Property="Orientation" Value="PortraitUp"/>
            <Setter Property="shell:SystemTray.IsVisible" Value="True"/>
        </Style>
    </Application.Resources>
    
    <Application.ApplicationLifetimeObjects>
        <shell:PhoneApplicationService />
    </Application.ApplicationLifetimeObjects>
</Application>

Page template

    <phone:PhoneApplicationPage
    x:Class="$namespace$.Page"
    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:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Style="{StaticResource DefaultPageStyle}"
    mc:Ignorable="d"
    d:DesignHeight="800"
    d:DesignWidth="480">

    <Grid Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel Margin="{StaticResource PageHeaderPanelMargin}">
            <TextBlock
                Text="{StaticResource ApplicationTitle}"
                Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock
                Text="page name"
                Margin="{StaticResource PageNameLabelMargin}"
                Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <Grid
            Grid.Row="1"
            Margin="{StaticResource PageContentPanelMargin}">
            
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>

You can also download zipped templates ready for importing into Visual Studio.
Just place them into “%my_docs%Visual Studio TemplatesItemTemplatesSilverlight for Windows Phone”.

Application and Page

Leave a Reply

Your email address will not be published. Required fields are marked *