InterDash Blogs

Sharing experience

WP8 and Win8 – Sharing Code

Today I started a new project with the idea of taking full advantage of the converging code base between Windows Phone 8 and Windows 8. The first step in this process was to establish a skeleton framework with a Portable Class Library to share the code.


http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714086(v=vs.105).aspx#BKMK_PortableClassLibrariesandMVVM

I like to establish directory structures to organize code so putting everything in the root of the Portable Class Library doesn’t work for me. In looking at the structure described above the ViewModels, Models, and code that can be shared go into the PCL. That all seems straightforward and here is what I came up with.

First Task

The Views are still platform specific, as is the startup. With that understanding we can begin by establishing resource strings in the PCL. There is no doubt in the benefit of sharing resource strings especially as more languages are supported.

Step 1 – Establish a Portable Class Library (PCL)

In the following our namespace is PCLExample1. In the example PicPresenter they added ‘Portable’ to the namespace. Since this is the code that is common to all it makes sense that its namespace is not decorated and serve as the starting point of our implementation.



After creating a new PCL I add Folders and delete the Class1.cs file


Within Resources I create Resource file named Strings.resx



Access the Properties of the newly created Resource and add the prefix Public to the ‘Custom Tool’ value and specify the ‘Custom Tool Namespace’ to be the same as the PCL.


Add an String to the Resource file named ‘AppTitle’.


 

Add a new class to the ‘Core’ folder called PortableStrings




Step 2 – The Windows Phone 8 Project

Add another project to the solution for the WP8 app.




Add the Reference to the PCL Solution


Include the xmlns for .Core and add the PortableStrings reference.



Change Main.xaml to reference the resource


Build to test. You should see the string in the designer.


Step 3 – The Windows 8 (Store) project



Add the reference to the PCL


Make the resource part of the application.




Step 4 – Shared Resources

Most images used in an application will not be platform specific but as copied resources they could be considered part of the views. Regardless, they don’t belong in a Class Library and it may be that you want a platform specific image.

Since Assets are not included in the PCL we will turn to a Linked Reference for our ‘shared’ content. This requires we establish a directory for holding our assets that will be shared and then reference files in those directories with links.


Copy in some images and then add a linked file reference, add code to view the image.




 

Add a Image to display in the Grid to demontstate the linked file in use.


Step 5 – RT APIs (Wasapi, etc.)

 

In this final and my next series of blog postings I will add an API for using Wasapi audio instead of XNA and implementing SQLite.

The extension for supporting Win RT in Window Phone 8 isn’t exactly the same so I expect a tricky implementation but for now we have a good start on using a PCL for dual development.

Updated: January 26, 2014 — 1:00 am

Leave a Reply

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

InterDash Blogs © 2014 Frontier Theme