Sharing code base between windows 8 & windows phone 8 apps

7
What does it mean by “ Sharing code base between Windows 8 & Windows Phone 8 apps Introduction:- With the release of Windows Phone 8, Microsoft had highlighted their plan of unifying the platforms by borrowing much of its code base, including kernel, from Windows 8. The result is that with the “8s” (all of the ‘8’ OS) and Visual Studio 2012, we can have one code base and generate apps for WP8, Windows RT, 32 bit x86 and 64 bit x86. Then, what does Microsoft mean by “same code base”? How can a developer achieve that? What are the benefits of using same code base? The developers are still not completely aware of how they can leverage this “surprising” feature in your Windows Phone 8/Windows 8 application development. 1) What does Microsoft mean by “same code base” ? The Windows Phone 8 release had a major development upgrade; it shares the same core as Windows; the Windows Phone Runtime API, which represents the managed API on Windows Phone 8, adopts many features of Windows Runtime API; and the introduction of development of native applications.All these points to the convergence of Windows Phone 8 and Windows 8, and this convergence helps to create flexible and powerful apps that will run on Windows Phone 8 and Windows 8 devices. The idea behind the code sharing is that the application logic will be separated from the presentation layer and this application logic can be shared among different platforms using some specific code sharing strategies, while the presentation layer will remain specific to each platforms. So there are some specific code sharing strategies that needed to be followed if we intend to implement platform portability in our apps. 2)How can a developer achieve maximum code reuse ? As Windows Phone shares it core with Windows, we developers have more opportunity than ever to leverage platform alignment to build their apps, while maximizing code reuse. Whether you are a Windows Store app developer or Windows Phone app developer or a developer

description

Document prepared by Anupreetha Rugmini

Transcript of Sharing code base between windows 8 & windows phone 8 apps

Page 1: Sharing code base between windows 8 & windows phone 8 apps

What does it mean by “ Sharing code base between Windows 8 & Windows Phone 8 apps ”

Introduction:-

With the release of Windows Phone 8, Microsoft had highlighted their plan of unifying the platforms by borrowing much of its code base, including kernel, from Windows 8. The result is that with the “8s” (all of the ‘8’ OS) and Visual Studio 2012, we can have one code base and generate apps for WP8, Windows RT, 32 bit x86 and 64 bit x86.

Then, what does Microsoft mean by “same code base”? How can a developer achieve that? What are the benefits of using same code base? The developers are still not completely aware of how they can leverage this “surprising” feature in your Windows Phone 8/Windows 8 application development.

1) What does Microsoft mean by “same code base” ?   The Windows Phone 8 release had a major development upgrade; it shares the same core as Windows; the Windows Phone Runtime API, which represents the managed API on Windows Phone 8, adopts many features of Windows Runtime API; and the introduction of development of native applications.All these points to the convergence of Windows Phone 8 and Windows 8, and this convergence helps to create flexible and powerful apps that will run on Windows Phone 8 and Windows 8 devices.

The idea behind the code sharing is that the application logic will be separated from the presentation layer and this application logic can be shared among different platforms using some specific code sharing strategies, while the presentation layer will remain specific to each platforms. So there are some specific code sharing strategies that needed to be followed if we intend to implement platform portability in our apps.

2)How can a developer achieve maximum code reuse ?   As Windows Phone shares it core with Windows, we developers have more opportunity than ever to leverage platform alignment to build their apps, while maximizing code reuse. Whether you are a Windows Store app developer or Windows Phone app developer or a developer new to these platforms, you can start creating apps targeting both Windows and Windows Phone.

To start developing those kind of apps, you need to install Visual Studio 2012 Pro along with Windows SDK 8.0 and Windows Phone SDK 8.0.

The different code sharing strategies/techniques are

Separate UI and application logic :-  The greatest potential to share code between Windows Phone 8 and Windows  8 apps lies in application logic. The design concepts and the XAML controls supported in Windows Phone 8 and Windows 8 are similar but it is not there we have to invest our time, rather we should spend in structuring our app which will make it reusable.

Page 2: Sharing code base between windows 8 & windows phone 8 apps

Model-View-View Model  is the recommended/best-practice pattern to be followed for     WindowsPhone and Windows Store apps and it is a way to separate our data from UI. The architecture of an app built using MVVM design pattern is

Model:  represents the data model that your app consumes.

View: Each page shown to the user is a view in MVVM terminology. The view is the XAML code used to define and style what the user sees.

ViewModel: The ViewModel ties the data model, or simply the model, to the UI, or views, of the app. It contains the logic with which to manage the data from the model and exposes the data as a set of properties to which the XAML UI, or views, can bind.

This means that design and develop the UI tailored to each platform to deliver the best possible user experience and the rest of the app can be either portable or common between the Windows Phone and Windows Store app. However, how much code you can share(even when using MVVM) will depend upon the complexity of our app and what API it uses.

Note :Portable code is any code that can be compiled once and run on windows Phone 8 or Windows 8  and Common code is code that uses API that is common to both platforms, but isn’t portable.

 Share functionality using Portable Class Libraries:-  Portable Class Libraries support a subset of .NET assemblies that target the platforms you choose. We can use them to create portable assemblies that can target multiple platforms including Windows 7, Windows 8, Windows Phone, SilverLight and XBox 360. This is a very good way to reduce time and cost by sharing functionality across the platforms we want to support.

Page 3: Sharing code base between windows 8 & windows phone 8 apps

There is a template available in Visual Studio 2012 Pro or greater versions to create a Portable Class Library. To reference a Portable Class Library , in Solution Explorer, select your project and then choose Add Reference. And when we create our app for both platforms, we need to identify the portable code; place this code in Portable Class Library and share the Portable Class Libraries between both the apps.But there are some characteristics for Portable Class Libraries, likea) it will support managed code only, b)it does not use conditional compilation c)it does not use Windows Runtime APIsd)it does not use UI constructs

So, If you need to implement functionality for Windows Phone 8 and implement it differently for Windows 8, you can’t include both code paths in a Portable Class Library. Instead, you should abstract away the platform-dependent code and share only the portable, platform-independent code. And in the case of Windows Runtime APIs also, you should abstract the use of Windows Runtime APIs into classes or objects that aren’t shared in a Portable Class Library.

Share code as “Add as Link” :- For the code that is non-portable -like the use of Windows Runtime API to harness the power of networking, proximity, in-app

Page 4: Sharing code base between windows 8 & windows phone 8 apps

purchases etc- we can abstract this code, which is common to both platforms, into a class that can be shared using “Add as Link” in Visual Studio.

“Add as Link” means, in Visual Studio we can use the same file in multiple projects, without copying the file to each project. This simple but powerful code sharing strategy can be   used to create a file or code asset once and then share it with multiple projects. Apart from being linked to your project , the file acts like any other file in the project, and when we edit the file, the changes are applied to all files that link to the file.

 The Windows Runtime API is not portable, but as the Windows Phone  and Windows share a subset of Windows Runtime API, we can just code against the API once, and then share the logic in both the apps.

The class SharedClass.cs contains code that isn’t portable, but is written using API common to Windows Phone 8 and Windows 8. We can share this class by adding it as a link to each project. That simply means we have abstracted the common, non-portable code into shared classes and each app project contains code and functionality that is specific to the app.

Sharing using Windows Runtime Components :- In addition to consuming the common Windows Runtime API, we can write our own Windows Runtime API to make our functionality available in all supported languages.

Page 5: Sharing code base between windows 8 & windows phone 8 apps

A Windows Runtime Component (also called 3rd-party Windows Runtime Component) is winmd file, which contains the metadata needed to project the functionality automatically

When to build and use Windows Runtime Component depends on so many factors :-

a) If our app carries out computationally intensive operations, we can opt to write that code in C++ to achieve optimal performance and if the code that uses this functionality is written in two different languages (C# and VB) we can package the functionality in Windows Runtime Component and call the component from our managed C# or VB code.

b) Windows Phone 8 delivers native API to enable powerful gaming scenarios, native networking and access to camera from native code. To use this API from managed code, we can wrap this functionality in Windows Runtime Component.

c) If we need to consume a third-party library written in another language, in our app, we can wrap that library in a Windows Runtime Component to grab that.

Windows 8 will allow us to create this Runtime Component in C++, C# and VB but Windows Phone 8 will only allow, C++ to create the same. Also, Windows Phone will allow these Components to be consumed in C++, C# and VB while Windows 8 will allow Javascript also, in addition to C++, C# and VB.

In addition to all the above technique we can use one more technique but is limited and should be used only for simple reusable components.

Sharing XAML UI :- The XAML implementations are not portable between the platforms. But you can isolate some of your custom basic UI building blocks into UserControls and share those classes as linked files that will be compiled for each platform. This technique is limited and should be used only for simple, reusable parts of your UI. The core of your UI should be built and tailored separately for each platform.

3) What are the advantages of using same code base?

1. Both the developers and the companies who develops these apps benefit from simultaneous development of Windows Phone 8 and Windows 8 apps. Building application for Windows 8 allows us to target desktop, laptop and tablet users and with Windows Phone developers we make sure the smart phone users also can use our application.

2. By developing simultaneously, not only we are able to write the code just once , but also to maintain a huge part of the application at one(centralized) place. This is very important and helpful in maintaining and extending applications.

3. Last but not the least, we can save a considerable amount of time and money.