Using Visual Studio to build Universal XAML Apps

Using Visual Studio to build Universal XAML Apps

Rate This
  • Comments 20

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:

New Project Templates

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.

'Add Windows Phone 8.1' command in Solution Explorer

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).

Separate projects for Windows 8.1 and Windows Phone 8.1

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.

Project Context Switcher

Intellisense

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.

Switch startup project in the debug target dropdown

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.

Improved Portable Class Libraries

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.

Class Library project templates for C

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.

image

Author: Navit Saxena, Program Manager

Navit Saxena is a Program Manager on the Visual Studio team. For over five years, Navit has been focused on XAML tooling in Visual Studio and Blend. He enjoys building Windows & Windows Phone apps and is always looking for ways to improve the XAML developer tools.

Leave a Comment
  • Please add 6 and 6 and type the answer here:
  • Post
  • Yes - but what's the point,? Very few people including developers want Windows phone and maybe even fewer for Windows 8.

    The big Windows market is  from Windows XP  (yes its still there) through to Windows 8.1 - We have never specifically targeted products just at the  latest version of Windows - it would be commercial suicide.

    What we want is the ability of Visual Studio to target multiple native platforms, including Android and ideally Apple with the minimum of changes - using c# or better still C++.

    Know VS is a Microsoft product but it isn't free we have to buy it - so how about making it really useful.

  • Great write up, thank you!

    I can attest that, although not recommended, you can put 100% of your XAML and .CS files into the shared project and have the app run in both Win 8 and WP8. Screens, SQLite classes, web service calls, the works. The only file I left in the app project was the app.xaml. The only problem I had was that Visual Studio crashed the further & harder I pushed the shared project...hopefully it was just my dev VM and not a global issue.

    Pretty amazing and welcomed!

  • Echoing Brian M.  

    I am looking at this in terms of the 2.0 version of our internal application.

    We are solid on Windows 7, no plans to push to 8 ( retraining costs )

    We allow our end users lots of variety in devices.

    Windows Phone is not something that our end users want.

    And frankly, we don't want the costs of having to push everyone to Windows Phone.

    So, this is a non-starter.  It sounds great, but it doesn't work were we need it to.

    The walled garden may be working for Apple for toy applications, but it doesn't for business and business apps.

    It's not a Windows only world out there.  Stop trying to tie us to it.

  • What is there new for Windows 7 desktop developers in XAML?   What new XAML tooling is there in VS 2012 after update 4?

  • Hi

    Thanks for your support. I know that many does not want to support you makings apps for Windows Phone. But personally I think it's great have that chance. I have two ideas (a game and ToDo app) that could use this very well.  

    It's a great opportunity for promote Windows Phone. Developers, if you like Android or iOS go for them, but also make great apps for Windows.

  • no windows 7 support, DOA!, wake up!

  • Our business applications run on Windows 7 and someday Windows 8 desktop and cannot be published through an app store given they are internal to our business.   What path for XAML/WPF and VS improvements are there for us?

    Our business application user base uses desktop and web based applications.   We do not have a standard mobile tablet/phone platform and do not develop for mobile.

    What is the forward path for a Fortune 500 business needing its data, applications and source code hosted in house on desktop applications or desktop browser based web sites (In other words, no cloud TFS, no cloud VS, no Azure, no mobile)?

    This is a common requirement for insurance, financial and health care corporations.

  • I do have a WP8.1 device (which I love!) and a laptop running 8.1, and maybe soon an 8.1 tablet so I am very excited by  these new development facilities.

    However, like nearly everyone that has posted here my bread and butter is WPF/XAML and WinForms business apps that run on Windows 7... why is Microsoft *only* concentrating on W8 when there is a ***massive*** worldwide community of businesses and  organisations who want to keep developing in Tools that are W7 compatible?  

    As much as I can't wait to get started on a universal app I have in mind, this is just for pleasure and not a realistic business solution (since businesses don't want W8 yet) so I am very concerned that MS (as usual) is putting all it's eggs in one very unpopular basket.  

    Given how MS has worked in the past I know this is probably a wasted request but *PLEASE* don't stop giving businesses and existing development environments new and exciting tools for existing technologies like WPf/XAML!!

  • Why Universal Windows Apps are not Enabled for Visual Basic!!?

  • Why, I do not support the universal application only Visual Basic. What Microsoft'm thinking. I would like you to respond as soon as possible.

  • I too would like to add my concern about this not being announced for Visual Basic.  I was at one of the Hackathon events and produced a nice little windows phone app using VB.  I originally tried using C#, but not using it on a regular basis I found that the syntax just got in the way.  I thought the whole idea was to be able to build on our existing skills, so don't understand why this thought would not be carried through with this product.

  • I'm a little disappointed by continued portable assembly approach.  Why didn't Microsoft implement .net 2.0 or .net 4.0 on all platforms and then add phone and tablet assemblies to it as required? -10 points for the added complexity and incompatibility issues using the current approach.  What is the expected lifecycle of universal apps?  Silverlight seems like the closest thing to universal development that Microsoft has right now.

  • @AbuS3ood, eightman, Vince Miccio,

    We’re currently in the process of building Universal app support for both languages for the next release of Visual Studio based on the .NET Compiler Platform (“Roslyn”).

    -The Visual Basic and C# Languages Team

  • Can u tell provide more insight on how  is "Tombstone" handled in Windows phone 8.1?

  • I can see the namespaces but not the assembly. If I develop a universal app then it seems logical to me to do it right from the start. So I do not want to use HERE but Bing right from the start. I assume some parts of WP8.1 are not ready yet? But when will they be ready? or better said what is the roadmap and please make it available with a non-expiring key, the same way as HERE has now.

Page 1 of 2 (20 items) 12