01 Oct

Sharing Code with Partial Classes

This is a multipart series on how to share code between .Net platforms.  All the examples will be showing how to share between Windows 8 Store applications and Windows Phone 8 application, but the techniques are useful for sharing code between any .Net/Xamarin platforms.

  1. Linked Files
  2. Conditional Compilation
  3. Partial Classes
  4. Inheritance
  5. Dependency Injection

What are Partial Classes?

Partial Classes are a way to split a class up into multiple files. It is frequently used when one part of your class is generated code and another part is written by hand.  In this case we’ll use partial classes to separate code that is shared between platforms and code that is platform specific.

How do I do it?

First you would need to use linked files to share the code between your projects. Then you need to change the class declaration to include the keyword partial.

partial class MainPageViewModel

Then you create a new files in each of your projects.  In this example we’ll create MainPageViewModel.WP8.cs and MainPageViewModel.W8.cs.  The WP8 file would be created in the Windows Phone project and the W8 file would be created in the Window 8 project.  We then need to include that same class declaration in those files.

Then we just move the methods with platform specific code into those platform specific partial classes.

The WP8 file would look like this:

partial class MainPageViewModel
{
    private void Load()
    {
        object name = null;
        if (IsolatedStorageSettings.ApplicationSettings.Contains("Name"))
        {
            name = IsolatedStorageSettings.ApplicationSettings["Name"];
        }
        if (name != null)
        {
            HelloMessage = "Load " + name.ToString();
        }
    }
}

The W8 file would look like this:

partial class MainPageViewModel
{
    private void Load()
    {
        object name = null;
        if (ApplicationData.Current.LocalSettings.Values.ContainsKey("Name"))
        {
            name = ApplicationData.Current.LocalSettings.Values["Name"];
        }

        if (name != null)
        {
            HelloMessage = "Hello " + name.ToString();
        }
    }
}

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>