Visual Studio 2012 provides an excellent set of tools for building Windows 8 and Windows Phone 8 applications. Therefore, it is appropriate to explore how much code can be shared between the version of the application's Windows store and the version of Windows Phone.
You can write Windows store applications in a variety of different languages: XAML goes with C #, Visual Basic, C + +, and even HTML5 with JavaScript.
You typically use XAML with C # or Visual Basic to write Windows Phone 8 applications, but the Windows Phone 8 SDK now supports the use of XAML and C + + to write Direct3D applications. Although the Windows Phone 8 SDK also provides templates for HTML5-based applications, they are simply based on XAML and hosting HTML5-based Web pages with the help of WebBrowser controls.
In this article, I'll explore three strategies for sharing code between Windows application stores and Windows Phone applications: Portable class Libraries (PCL), Windows Runtime (WinRT) components (and Windows Phone Runtime Components), and Vis UAL Studio's Add as Link option. You can find more guidance in the Developer Center (http://aka.ms/sharecode) about sharing code between Windows App Store and Windows Phone application.
It is noteworthy that while there are many similarities between Windows Store applications and Windows Phone applications, such as dynamic tiles, they are, after all, different platforms, and therefore should be specifically designed to tailor UX.
Architecture
In general, the architectural principles that promote problem segmentation can increase the proportion of shareable code. If you have adopted a pattern that promotes problem segmentation, such as a model-view-view model (MVVM) or model-view-Controller (MVC), it is easier to implement code sharing, and the dependency injection pattern in the architecture also helps with code sharing. When implementing architectural design for new applications, it is important to consider adopting such patterns to improve the level of code sharing that is achievable. For existing applications, you can consider refactoring the architecture to facilitate problem segmentation and thus facilitate code sharing. MVVM or MVC also has other advantages, such as allowing designers and developers to work at the same time, in addition to achieving problem segmentation. Developers can write UX-specific implementation code in Visual Studio while designers use tools such as Expression Blend for UX design.
Portable class Libraries
The PCL project in Visual Studio 2012 enables Cross-platform development, which allows you to select the target framework to support for the generated assembly. The PCL project template introduced as an optional add-in in Visual Studio 2010 is now included in Visual Studio Professional 2012 and later.
So, what code can be shared in PCL?
PCL is called PCL because it enables the sharing of portable code, and for code to be portable, it must be managed code written in C # or Visual Basic. PCL generates only a single binary file, so the portable code does not use conditional compilation directives, but rather abstracts platform-specific functionality with an interface or abstract base class. When portable code needs to interact with platform-specific code, you can use dependency injection patterns to provide an abstract platform-specific implementation. After compilation, PCL generates a single assembly for any project references that are based on the target framework.
Figure 1 shows a suggested architectural approach to implementing shared code with PCL. In MVVM mode, the view model and model are included in PCL with the abstraction of any platform-specific functionality. Windows Store and Windows Phone applications provide an abstraction of the startup logic, views, and implementations of any platform-specific functionality. Although the MVVM design pattern is not necessary to implement portable code, the problem segmentation advocated by this pattern can produce a clean, extensible architecture.
Figure 1 Sharing code with MVVM design pattern