Pages

Saturday, March 22, 2014

Today it is the second day of 2 day Imagine Camp series. Deliver a session under the subject Portable Code Strategies for Windows Store Apps and Windows Phone 8 Apps and it's the same session which I delivered on 1st day of Imagine Camp.

In this session the idea was to show how we can write and use a single code base which can be consumed from Windows Store Apps as well as from Windows Phone 8 Apps.

Friday, March 21, 2014

In this post let’s see how we can use Portable Class Libraries and abstract classes to design a common code base for Windows Store and Windows Phone 8 App development.

Portable Class Library

Portable Class Library project template was initially introduced with Visual Studio 2012 and this is not available in Visual Studio Express Editions. The Portable Class Library project enables you to write and build portable assemblies that work without modification on multiple platforms, such as Windows 7, Windows 8, Silverlight, Windows Phone, and Xbox 360. You can create classes that contain code you wish to share across many projects, such as shared business logic, and then reference those classes from different types of projects.

Best example would be use of HttpClient to bind a online XML Data source to a ListView in Windows Store App or to a LongListSelector in Windows Phone 8 App. Now let’s see this example in action.

I have the following XML file hosted in Local IIS.

<company>

<employee>

<employeeid>1</employeeid>

<firstname>Jaliya</firstname>

<lastname>Udagedara</lastname>

<age>27</age>

</employee>

<employee>

<employeeid>2</employeeid>

<firstname>Martin</firstname>

<lastname>Smith</lastname>

<age>15</age>

</employee>

<employee>

<employeeid>3</employeeid>

<firstname>Jane</firstname>

<lastname>Doe</lastname>

<age>27</age>

</employee>

<employee>

<employeeid>4</employeeid>

<firstname>David</firstname>

<lastname>Smith</lastname>

<age>22</age>

</employee>

</company>

I am opening up Visual Studio and creating a project of type “Portable Class Library”. I am naming the project name as “PCLCommon” and the solution name as “PCLAndPlatformAbstractionDemo”.

Portable Class Library

In the next window, I am selecting the following target frameworks.

Target Frameworks

Inside the project, I am adding two folders named “Model” and “ViewModel”. Inside the “Model” folder, I am adding a new class named “Employee”.

Employee.cs

namespace PCLCommon.Model

{

publicclass Employee

{

publicint EmployeeId { get; set; }

publicstring FirstName { get; set; }

publicstring LastName { get; set; }

publicint Age { get; set; }

}

}

Inside the “ViewModel” folder, I am adding a new class named “EmployeeViewModel” which implements INotifyPropertyChanged interface.

Here, I have a ObservableCollection of type “Employee” named “Employees” and I have a method called “LoadData” which reads the locally hosted XML file and inserts “Employee” entities to my “Employees” ObservableCollection.

Now I am adding two projects to the solution, one is a Windows Phone App and the other is a Windows Store App. From both these project I am adding a reference to my “PCLCommon” project.

Add Rerefence

Once I do that, I am modifying the MainPage.xaml in both Windows Phone 8 App and Windows Store App.

Now inside the code behind of MainPage.xaml in both projects, I am writing the following code.

Windows Phone 8 App and Windows Store App – MainPage.xaml.cs

async void MainPage_Loaded(object sender, RoutedEventArgs e)

{

EmployeeViewModel viewModel = new EmployeeViewModel();

await viewModel.LoadData();

DataContext = viewModel;

}

Now when I run both these projects, I am getting the desired output.

Windows Pone 8 App

Windows Store App

Abstract Class

Let’s say there is a need that from my Portable Class Library, I need to save some values in Local Settings in both Windows Phone and in the device running Windows Store App. There I am facing a problem, because accessing Local Settings in both these platforms are different from one to another. But I should be able to access the Local Settings in Windows Phone and in the device running Windows Store App in the same manner regardless of the platform. For these kind of scenarios, I can use abstract classes.

To demonstrate the use of abstract classes in writing cross platform applications, I am modifying the above solution further.

In my “PCLCommon” project, I am adding a new abstract class named “SettingManager”.

SettingManager.cs

namespace PCLCommon

{

publicabstractclass SettingManager

{

publicabstractvoid SetLocalValue(string key, objectvalue);

publicstatic SettingManager Instance { get; set; }

}

}

Now in my Windows Phone 8 App and in my Windows Store App, I am adding a class named “SettingHelper” and I am deriving the class from “SettingManager” in “PCLCommon” project.

Windows Phone 8 App – SettingHelper.cs

using PCLCommon;

using System.IO.IsolatedStorage;

namespace WindowsPhone8App

{

publicclass SettingsHelper:SettingManager

{

publicoverridevoid SetLocalValue(string key, objectvalue)

{

IsolatedStorageSettings.ApplicationSettings.Add(key, value);

}

}

}

Windows Store App – SettingHelper.cs

using PCLCommon;

using Windows.Storage;

namespace WindowsStoreApp

{

publicclass SettingsHelper:SettingManager

{

publicoverridevoid SetLocalValue(string key, objectvalue)

{

ApplicationData.Current.LocalSettings.Values[key] = value;

}

}

}

As you can see, inside the "SettingHelper" class in Windows Phone 8 App and Windows Store App, I have overrided the “SetLocalValue()” method in “SettingManager” with platform specific functionality.

Now inside the code behind of MainPage.xaml in both Windows Phone 8 and Windows Store App projects, I am writing the following code.

Now from my Portable Class Library, I can write the following code to insert values to Local Settings.

SettingManager.Instance.SetLocalValue("SomeKey", "SomeValue");

When I am running the Windows Phone 8 app, the instance in “SettingManager” would be Windows Phone 8 Apps’ implementation of “SettingManager”. When I am running the Windows Store app, the instance would be Windows Store Apps’ implementation of “SettingManager”. So to the respective Local Settings, data will be inserted.