May 052014
 May 5, 2014  Posted by at 3:05 pm C# & F#, Universal Apps, WinRT, WP, XAML  Add comments

(video) Sharing views (XAML) using Portable Class Libraries

After another PCL debate on Twitter I realized many were unaware of the fact that PCLs have undergone a lot of improvements, and for example fully supports sharing views and XAML resources between Windows Store Apps and Windows Phone (WinRT) apps.

Here is a demo and some rambling. Happy coding!

Big thank you to David Kean (Microsoft) for answering all my questions and kindly helping me with the App.xaml issue 🙂 You rock!

Steps in the demo:

Create a solution

Add a Portable Class Library with reference to only Windows 8.1 and Windows Phone 8.1, delete the class file

Add a Windows Store App, Blank template, to the solution

Add a WP, Blank template, to the solution

Add a reference to the Portable Class Library from both the projects

Add a new item to the Portable Class Libray, a Blank Page. Call it MainPage (for this demo). For demonstration purpose I’ve changed the background to yellow. Although I forgot to delete the MainPage from the two projects in the video you can of course safely delete them.

You might get some errors and red text if you have resharper installed. If it compiles ignore it or turn of resharper.

Create a base class in the Portable Class Library, inherit from the application class and add startup code (basically the code below the constructor in the App.xaml.cs file of the Windows Store App). Add namespaces needed

Delete the code in App.xaml.cs that we added to the shared class in the Portable Class Library

Let the App.xaml.cs inherit from the base class

Edit the App.xaml to reference and use the Portable Class Library (so it’s the same class definition)

And voila. It should all work.

  2 Responses to “(video) Sharing views (XAML) using Portable Class Libraries”

  1. Very important info that the .NET Framework shouldn’t be included in the Portable Class Library (PCL). I was struggling with my PCL getting access to the Windows API. Thank you.

  2. Hi Iris,
    While searching for sharing ResourceDictionary from PCL I stumbled on this episode. Nice work. You actually made an universal app (Win8 & wp8).
    Keep up the good work.

 Leave a Reply

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=""> <s> <strike> <strong>



What is 15 + 13 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)