At the Build conference, we announced the release of the new converged Windows Phone 8.1 and Windows 8.1 platforms. As a developer, this means you can now build XAML and HTML universal apps that run on both Phone and Tablets by sharing a significant amount of code and content. To enable building universal apps, we added a number of new features to Visual Studio as part of the Visual Studio Update 2 RC.
You have two ways to learn more about these features. One way is through this blog post. The other way is by watching my Build talk that covers all of the material you will see here in more detail:
There is no right or wrong way here, so pick either the video or the blog depending on how much time you have. Without further delay, let’s take a quick look at universal apps!
Creating Universal Apps
To help you get started with building universal apps in C#, C++, and JS, we created new project templates that contain the basic structure and behind-the-scenes configurations to allow you to share code and content:
If you already have an existing Windows 8.1 application, you can use the “Add Windows Phone 8.1” command to add a new Windows Phone 8.1 project and a shared project to the solution. A similar option is also available if you have a Windows Phone 8.1 application, and wanted to add support for Windows 8.1.
Structure of Universal Apps
A universal app is a collection of three projects – a Windows Store project, a Windows Phone project and a Shared project – enclosed in a solution folder that is optional. The Windows Store and Windows Phone projects are platform projects and are responsible for creating the application packages (.appx) targeting the respective platforms. These projects contain assets that are specific to the platform being targeted.
The Shared project contains assets that are shared between the Windows Store and Windows Phone projects. The set of item types (.cs, xaml, .xml, .png, .resw, etc.) supported by the shared projects is the same as the platform projects. Shared projects by themselves don’t have a binary output but their contents are imported by the platform projects and used as part of the build process to generate the Windows Store and Windows Phone application packages (.appx).
Writing code in the Shared project
While developing your universal app, you will mostly be writing code that runs on both platforms. If required, you can also write platform specific code in the Shared projects using #if and #endif directives. By default, we have predefined the following conditional compilation constants that you could use to write platform specific code.
C# | WINDOWS_APP | WINDOWS_PHONE_APP |
C++ | WINAPI_FAMILY_PC_APP | WINAPI_FAMILY_PHONE_APP |
Context switcher in the editor
While writing code in a Shared project, you can use the project context switcher in the navigation bar to select the platform you are actively targeting, which in turn drives the intellisense experience in the code editor.
Switching startup projects using debug target dropdown
We have also added the ability to quickly switch the startup projects in the debug target dropdown that now enumerates all the possible projects in the solution that you might want to deploy to a device or emulator/simulator.
Sharing code across Universal Apps
You can use class libraries to share your code across different universal apps. For C# and Visual Basic, we have improved the existing Portable Class Libraries (PCLs) to also support Windows Runtime and XAML when targeting Windows 8.1 and Windows Phone 8.1 platforms. Check out this blog for more details on PCL improvements.
For C++, you can use the new Class Library project templates under “Universal Apps” with shared projects to share your code between Windows 8.1 and Windows Phone 8.1 class libraries.
I hope you found this overview of building XAML universal apps useful. If you have any questions or comments, please feel free to post below or contact us via forums or UserVoice . Stay tuned for another blog explaining the new XAML tooling features we have added in Visual Studio to support Windows Phone 8.1 applications.