Iris Classon
Iris Classon - In Love with Code

(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 toDavid 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.

Comments

Leave a comment below, or by email.
Frederik Krautwald
5/19/2014 9:47:26 AM
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. 
Ismail
2/27/2016 9:20:29 AM
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. 


Last modified on 2014-05-05

comments powered by Disqus