Sharing Code Part I: Common Source Files

In this article, I’ll go through how to share source code files between Windows Phone 8, Windows Phone 7.1 and Windows 8.  In this sample we’ll go through how to share a Math class between two separate Windows Phone projects.

Creating the Projects

First thing is to create two Visual Studio 2012 Projects one for Windows Phone 7.1, 8 and Windows 8.

Windows Phone 7.1 Project

Create a Windows Phone Project and call it WP7_App and uncheck ‘Create Directory for solution’.

1

In the next dialog select Windows Phone OS 7.1

2

At this point your project should be generated.

Windows Phone 8 Project

Now we need to create a Windows Phone 8 project and essentially, these are the same steps as the 7.1 project.

Open a new Visual Studio 2012 window and create a new Windows Phone Project. Name the project WP8_App and uncheck Create directory for solution.

3

In the next dialog, select Windows Phone 8.0 as the target platform.

4

Click ok and the Windows Phone 8 project will be created.

Windows 8

Next we need to create our Windows 8 project. Open a new Visual Studio 2012 window and create a Windows 8 C# project. Name the project W8_App and uncheck Create directory for solution.

5

Now you should have three projects in the same directory as follows

6

Next we’ll actually start sharing some code.

The Math Class

In this example we’ll be creating a sample math class that will be used and expanded on in future parts. We’ll be using a singleton pattern for the class for ease of use throughout the different platforms.

In the WP7_App project, add a class called Math.cs. Change the namespace to Common and add the following code.

public class Math2
{
    private static Math2 _default;
    public static Math2 Default
    {
        get
        {
            if (_default == null)
                _default = new Math2();
            return _default;
        }
    }

    private Math2()
    {
    }

    public int Add(int x, int y)
    {
        return x + y;
    }

    public int Subtract(int x, int y)
    {
        return x - y;
    }

    public int Multiple(int x, int y)
    {
        return x * y;
    }

    public int Divide(int x, int y)
    {
        return x / y;
    }

}

As you can see it’s a very simple class that performs some simple math operations. Next we’ll need to add some code to call this class from the UI.

Updating the UI

In the WP7_App, WP8_App & W8_App projects. Open up MainPage.xaml and update the ContentPanel xaml in the Windows Phone projects with the following

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" VerticalAlignment="Top">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBlock Text="x" VerticalAlignment="Top"></TextBlock>
    <TextBox InputScope="Number" Grid.Row="0" Name="x"></TextBox>
    <TextBlock Text="y" Grid.Row="1" VerticalAlignment="Top"></TextBlock>
    <TextBox InputScope="Number" Grid.Row="1" Name="y"></TextBox>
    <Grid Grid.Row="2">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Button Name="btnAdd" Grid.Column="0">+</Button>
        <Button Name="btnSubtract" Grid.Column="1">-</Button>
        <Button Name="btnMultiply" Grid.Column="2">x</Button>
        <Button Name="btnDivide" Grid.Column="3">/</Button>
    </Grid>
    <TextBlock Name="lblResult" Grid.Row="3" TextAlignment="Center">[Result]</TextBlock>
</Grid>

In the Windows 8 project, you will be replacing the main grid on the page.

On Windows Phone, the changes will result with the following.

7

On Windows, this will result in the following

8

The UI is not the greatest, but will leave it to reader to fix.

Code Behind

Open up MainPage.xaml.cs in all three projects and add the following helper properties

private int X
{
    get
    {
        return Int32.Parse(x.Text);
    }
}

private int Y
{
    get
    {
        return Int32.Parse(y.Text);
    }
}

Now add the following to the constructor

btnAdd.Click += (o, e) =>
{
    lblResult.Text = Common.Math2.Default.Add(this.X, this.Y).ToString();
};

btnSubtract.Click += (o, e) =>
{
    lblResult.Text = Common.Math2.Default.Subtract(this.X, this.Y).ToString();
};

btnMultiply.Click += (o, e) =>
{
    lblResult.Text = Common.Math2.Default.Multiple(this.X, this.Y).ToString();
};

btnDivide.Click += (o, e) =>
{
    lblResult.Text = Common.Math2.Default.Divide(this.X, this.Y).ToString();
};

Run the WP7_App project, add some numbers for X & Y and click one of the buttons. If you try to run on Windows 8 or Windows Phone 8, you will get compile errors which we will fix in the next section.

NOTE: I will leave it up to you to add error handling.

Sharing the Code

Now that all the projects and code are setup we are ready to use our first technique for code sharing which is sharing the Math.cs file across all platforms.

Note: Technically I did use a code sharing technique across all platforms called copy paste of code. This is not a technique you want to use because maintenance of the code will be a nightmare.

In the WP8_App project click on Project – Add Existing Item and navigate to the WP7_app project to find the Math.cs file. Select the file, then on the Add button click the dropdown and select Add As Link. Do the same in the W8_App project.

Now when you run the project on Windows Phone 7 or 8 and Windows 8, the projects will compile and run.

The advantage of this, one code change you make to the Math.cs all projects will get the changes. You will require a re-compile, but you will have less code to maintain. You can download the source code from github.

Up Next

In the next part, we’ll look at using Partial Classes to try and share some more code and we’ll look at some of the UI Code.

 


Warning: count(): Parameter must be an array or an object that implements Countable in /home/usnbis1maldq/domains/markarteaga.com/html/wp-includes/class-wp-comment-query.php on line 405