Creating a portable library

Most of us have often struggled and hacked our code to implement an assembly that we could use in different .NET target platforms. Portable libraries are here to help us to do exactly this.

Now there is an easy way to develop a portable assembly that works without modification in .NET Framework, Windows Store apps style, Silverlight, Windows Phone, and XBOX 360 applications.

The trick is that the Portable Class Library project supports a subset of assemblies from these platforms, providing us a Visual Studio template.

This recipe will show you how to implement a basic application and help you get familiar with Visual Studio 2012.

Getting ready

In order to use this recipe you should have Visual Studio 2012 installed. Note that you will need a Visual Studio 2012 SKU higher than Visual Studio Express for it to fully support portable library projects.

How to do it...

Here we will create a portable library and see how it works:

  1. First, open Visual Studio 2012 and create a new project. We will select the Portable Class Library template from the Visual C# category.
  2. Now open the Properties dialog box of our newly created portable application and, in the library we will see a new section named Target frameworks. Note that, for this type of project, the dialog box will open as soon as the project is created, so opening it will only be necessary when modifying it afterwards.
    How to do it...
  3. If we click on the Change button, we will see all the multitargeting possibilities for our class.
    How to do it...
  4. We will see that we can target different versions of a framework. There is also a link to install additional frameworks. The one that we could install right now is XNA but we will click on Cancel and let the dialog box be as it is.
  5. Next, we will click on the show all files icon at the top of the Solution Explorer window (the icon with two papers and some dots behind them), right-click on the References folder, and click on Add Reference. We will observe on doing so that we are left with a .NET subset of assemblies that are compatible with the chosen target frameworks.
    How to do it...
  6. We will add the following lines to test the portable assembly:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace pcl_myFirstPcl
    {
    public static class MyPortableClass
        {
    public static string GetSomething()
            {
    return "I am a portable class library";
            }
        }
    }
  7. Build the project.
  8. Next, to try this portable assembly we could add, for example, a Silverlight project to the solution, together with an ASP.NET Web application project to wrap the Silverlight.
  9. We just need to add a reference to the portable library project and add a button to the MainPage.xaml page that calls the portable library static method we created.
  10. The code behind it should look as follows. Remember to add a using reference to our portable library namespace.
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using pcl_myFirstPcl;
    
    namespace SilverlightApplication_testPCL
    {
    public partial class MainPage : UserControl
        {
    public MainPage()
            {
    InitializeComponent();
            }
    
    private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                String something = MyPortableClass.GetSomething();
    MessageBox.Show("Look! - I got this string from my portable class library: " + something);
            }
        }
    }
  11. We can execute the code and check if it works.
  12. In addition, we could add other types of projects, reference the Portable Library Class, and ensure that it works properly.

How it works...

We created a portable library from the Portable Class Library project template and selected the target frameworks.

We saw the references; note that it reinforces the visibility of the assemblies that break the compatibility with the targeted platforms, helping us to avoid mistakes.

Next we added some code, a target reference application that referenced the portable class, and used it.

There's more...

We should be aware that when deploying a .NET app that references a Portable Class Library assembly, we must specify its dependency to the correct version of the .NET Framework, ensuring that the required version is installed.

A very common and interesting usage of the Portable Class Library would be to implement MVVM. For example, we could put the View Model and Model classes inside a portable library and share it with Windows Store apps, Silverlight, and Windows Phone applications. The architecture is described in the following diagram, which has been taken from MSDN (http://msdn.microsoft.com/en-us/library/hh563947%28v=vs.110%29.aspx):

There's more...

It is really interesting that the list of target frameworks is not limited and we even have a link to install additional frameworks, so I guess that the number of target frameworks will eventually grow.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset