Platform Abstraction with the Portable Class Library

.NET Framework 4.5

When you develop for multiple platforms by using the .NET Framework, the Portable Class Library helps you maximize code reuse. However, sometimes you'll need to abstract out platform-specific code to avoid circular references. This article provides an example.

Frequently, you'll want to call non-portable members from a portable class. You can't call non-portable members directly, because those members aren't available in the Portable Class Library project. Instead, you can create an abstract class in the Portable Class Library project and use that class from all portable code.

The abstract class should declare abstract methods that you need to use in the portable code and a public static property that is an instance of itself, which you'll use to initialize the class in the platform-specific code.

In the platform-specific project, you create a child class of the abstract class and implement the methods with platform-specific behaviors. You also initialize the platform-specific instance so that it can be used by the portable code.

The following example shows a portable class for working with local settings. Local settings are handled differently in a Windows Store app and a Silverlight app, so the portable class doesn't provide any implementation details. The implementation is provided in the platform-specific derived classes.

using System;

namespace ExamplePortableLibrary
{
    public abstract class ExampleLocalSettings
    {
        public abstract void SetLocalValue(string name, object value);

        public static ExampleLocalSettings Instance { get; set; }
    }
}

In your Silverlight project, you add a reference to the portable assembly, and then create a child class that implements the local setting operation that's specific to the Silverlight environment. This example shows a Silverlight implementation of the ExampleLocalSettings class:

using System;
using System.IO.IsolatedStorage;
using ExamplePortableLibrary;

namespace SilverlightApplication1
{
    class SilverlightImplementation : ExampleLocalSettings
    {
        public override void SetLocalValue(string name, object value)
        {
            IsolatedStorageSettings.ApplicationSettings.Add(name, value);
        }
    }
}

In your Windows Store app, you add a reference to the portable assembly, and then create a child class that implements the local setting operation that's specific to Windows Store apps. This example shows an implementation of the ExampleLocalSettings class for a Windows Store app:

using System;
using Windows.Storage;
using ExamplePortableLibrary;

namespace App1
{
    class AppImplementation : ExampleLocalSettings
    {
        public override void SetLocalValue(string name, object value)
        {
            ApplicationData.Current.LocalSettings.Values[name] = value;
        }
    }
}

In both the Silverlight app and the Windows Store app, you must initialize the specific implementation of the child class and set it to the Instance property. Typically, you create this instance when the app starts. This example shows how to initialize the Silverlight implementation:

ExampleLocalSettings.Instance = new SilverlightImplementation();

Here's how you initialize the implementation for the Windows Store app:

ExampleLocalSettings.Instance = new AppImplementation();

In your Portable Class Library project, you can use the Instance property to call the SetLocalValue method as follows:

ExampleLocalSettings.Instance.SetLocalValue("ExampleSetting", "New value to add");
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft