DVLUP
May 27, 2015

One XAML for UWP, Windows 8.1 and Windows Phone 8.1

Posted on May 27, 2015  •  4 minutes  • 646 words
Table of contents

I was recently involved in a conversation on how to have the easiest way to maintain a XAML view across a UWP (Universal Windows Project) a Windows 8.1 and Windows Phone 8.1 projects. There are a few options and the most straight forward way to do it with a UserControl residing in a Portable Class Library. However…

What if you wanted to have **tailored code for each **without littering your code with #ifdef but still share a XAML view?  The XAML View might be what you’re looking for. This is a tutorial on how to do just that. An example project is available for download at the end of the article. Let’s get started:

Step 1:

Create a new 8.1 Universal app

<img src="/wp-content/uploads/2015/05/tpx1.png" alt="tpx1">

Step 2:

Move the Shared, Windows and Windows Phone projects out of the virtual folder and delete the folder (you can cut/paste or just click and drag them). The solution should look like this now

<img src="/wp-content/uploads/2015/05/tpx2.png" alt="tpx2">

(Note: If you’re doing this to a solution that has many other projects, you might want to skip this step and add the UWP project to the virtual folder instead)

Step 3:

Add a new UWP project to the solution and name it the same as the 8.1 app, but with the Universal suffix.

<img src="/wp-content/uploads/2015/05/tpx3.png?w=700" alt="tpx3">

(Your solution should look like this now)

<img src="/wp-content/uploads/2015/05/tpx4.png" alt="tpx4">

Step 4:

Add a “Views” folder to each of the projects

<img src="/wp-content/uploads/2015/05/tpx5.png?w=700" alt="tpx5">

Step 5:

Within the Universal project, right click and Add > New Item > XAML View

<img src="/wp-content/uploads/2015/05/tpx6.png?w=700" alt="tpx6">

Step 6:

Move the new XAML View to the Shared project’s Views folder and change the namespaces in the view to match

<img src="/wp-content/uploads/2015/05/tpx7.png?w=700" alt="tpx7">

Step 7:

Delete App.xaml from the Universal project and add a reference to the solution’s Shared project (Note: this is in the new Shared projects references section)

<img src="/wp-content/uploads/2015/05/tpx8.png?w=700" alt="tpx8">

Step 8:

Here’s where the magic happens. We’ll be adding a code behind for this view in each of the projects! I’ll break this down into sub-steps:

  1. Add a new class to the Windows 8.1 project’s Views folder (Add > New Item), name it as if it were the code-behind for the view. In this case it would be SharedPage.xaml.cs
  2. Change the namespace of the class to Views
  3. Add the public, sealed and partial modifiers to the class
  4. You’ll next need to inherit from the Page class
  5. Add a page constructor
  6. Now you can copy and paste this new class into each of the platform projects (remember, we do not need it in the Shared project)

This is what it should look like

<img src="/wp-content/uploads/2015/05/tpx9.png?w=700" alt="tpx9">

Step 9:

You’ll need to quickly pop into the Build **Configuration Manager **and check off Build and Deploy for the Universal app (you can find Configuration Manager in the target dropdown or in the Build menu)

<img src="/wp-content/uploads/2015/05/tpx10.png?w=700" alt="tpx10">

Step 10:

Lastly, for the purposes of this demo, go to App.xaml.cs and change the initial launch target (MainPage) to be the new shared page (SharedPage). I could have put a button on MainPage for each app, but let’s keep this tutorial as short as possible.

<img src="/wp-content/uploads/2015/05/tpx11.png?w=700" alt="tpx11">

Final result!

This is the same XAML View compiled with different code-behind files :)

<img src="/wp-content/uploads/2015/05/tpx12.png?w=700" alt="tpx12">

(NOTE: I put a TextBlock on the SharedPage and update the text in each constructor to show which platform launched it. Here are the WP8.1 emulator, Windows 10 PC and Windows 10 Mobile Emulator running their apps all showing the same XAML view).

Source Code

Download the Sample App Source Code From Here

Bonus:

The Telerik Universal Control can be used in the shared page as long as each of the projects have a reference to the Telerik UYI for Windows Universal DLLs. Send me a tweet and show me what you’ve done, I’ll RT your awesomeness!

Follow me on your preferred social network =>