04 |sharing code between windows 8 and windows phone 8 in visual studio ben riga

26
04 | Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga http://about.me/ben.riga

Upload: elfreda-shields

Post on 17-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

Sharing Code Between Windows Phone 8 and Windows 8 in Visual Studio

TRANSCRIPT

Page 1: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

04 | Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio

Ben Rigahttp://about.me/ben.riga

Page 2: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Building Apps for Both Windows 8 and Windows Phone 8 Jump Start

01 | Comparing Windows 8 and Windows Phone 8

02 | Basics of View Models

03 | MVVM ( (Model-View-ViewModel)

04 | Sharing Code

Course Topics

Page 3: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Reuse techniquesPortable libraryShared source codeInheritanceConditional compilationPartial classes and methods

Q&A

Agenda

Page 4: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Reuse Techniques

Page 5: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Portable Class Library project template in Visual Studio 2012 (except Express)C# or Visual BasicGood for creating a cross-platform .dll with pure business logicOnly managed codeSingle codebase for Windows, Silverlight, Windows Phone, or XboxOnly shared assemblies are available for use (mostly restricted to System namespaces)Overall, limited, but provides portable, modular, and encapsulated code

Portable Library

Page 6: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Demo 1:Portable Class Library

Page 7: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Share code files between multiple projectsA single change shows up in all projectsUse Add As Link to include an external code file in multiple projects

When the APIs don’t match, use another technique to separate platform-specific code, such as: inheritance, #if conditional blocks, or partial classes and methods.

Shared Source Code

Page 8: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Enable/disable pieces of code based on target platform (or any other compiler variable)Platform-specific compilation constants:NETFX_CORE Windows 8WINDOWS_PHONE Windows Phone 8

Useful for when there are subtle differences in the APIsIf overused, code can become unreadable and hard to maintain

#if Conditional Blocks

Page 9: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

#if Conditional Block Examples (1/2)#if NETFX_CORE

using Windows.UI.Xaml.Media.Imaging;#else

using System.Windows.Media.Imaging;#endif 

Page 10: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

#if Conditional Block Examples (2/2)public object Convert(object value, Type targetType, object parameter,

#if !NETFX_CORECultureInfo culture

#elsestring language

#endif)

Page 11: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Demo 2:#if Conditional Blocks

Page 12: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Put shared functionality in base class and platform-specific code in sub-classesSeparates cross-platform features from platform-specific featuresBase class can be abstract to enforce platform-specific implementations

Two main approaches:The base class contains fully implemented logic. The sub-classes provide additional platform-specific functionality.The base class is incomplete. Subclasses implement the abstract methods to provide platform-specific functionality.

Inheritance

Page 13: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

/// <summary>/// Base View Model used across both platforms/// </summary>public class AlbumPageViewModel : NotifyBase, IViewModel{ ... }/// <summary>/// Sub-class containing additional information used in the Windows application/// </summary>public class WinAlbumPageViewModel : PhotoCollectionViewModel, ISupportsDesignTimeDataViaCode{} 

Inheritance Examples (1/2)

Page 14: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

/// <summary>/// Abstract base class available across both platforms/// </summary>public abstract class StartPageViewModel : NotifyBase,IViewModel{ protected abstract bool NavigatedToPhoto();}/// <summary>/// Sub-class implementing the abstract method/// </summary>public class WinStartPageViewModel : StartPageViewModel, ISupportsDesignTimeDataViaCode{ protected override bool NavigatedToPhoto() { NavigationService.Navigate<WinPhotoCollectionViewModel>(SelectedPicture.Path); return true; }}

Inheritance Examples (2/2)

Page 15: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Demo 3:Inheritance

Page 16: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Shared functionality in one code fileEx: DataModel.cs

Platform-specific code in another code fileEx: DataModel.WP8.cs

Partial class definitions compiled into a single class

Partial Classes

Page 17: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Can use partial methods as a mechanism to separate out platform-specific logicTwo parts: the definition and the implementationBoth parts must be in the same partial classIf no implementation exists, the compiler removes all calls to the method

Partial Methods

Page 18: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Partial Classes Example/// <summary>/// DataModel.cs/// </summary>public partial class DataModel:IDataModel { public async Task<IEnumerable<IFolder>> GetFoldersAsync(IFolder root) { // ... var folders = await LoadFolders(root); // ... return folders }}/// <summary>/// DataModel.WP8.cs/// </summary>public partial class DataModel { private async Task<IEnumerable<IFolder>> LoadFoldersAsync(IFolder root) { // ... }} 

Page 19: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Demo 4:Partial Classes

Page 20: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Recap

Page 21: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Develop apps for both Windows Phone 8 and Windows 8!Even if you do not initially target both platforms, it does not hurt to plan for the future.Be aware of which APIs are common to both platforms and which are not.

Use DataBinding to make your life easier when creating complex UIs that need to be dynamically updated with data.Use commands to execute a piece of code when a button is clickedMVVM works well with data binding

Takeaways

Page 22: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Consider designing your apps around the MVVM pattern.Even if you do not use MVVM, separate the data model from the UI. This lets you reuse the data model in the future.

Use strategies so that you can reuse source codePortable librariesShared source files#if conditional blocksInheritancePartial classes, partial methods

Takeaways

Page 23: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Windows 8, Windows Phone 8 APIsWindows Phone API QuickStart

Data BindingData Binding OverviewXAML Data binding sample

MVVM“Using the MVVM Pattern in Windows”“MVVM Light Toolkit”

Additional ResourcesSharing CodeWindows Phone 8 and Windows 8 app development

Sample Code“Windows 8 app samples”“Windows Phone samples”

All resources available at: http://bit.ly/BuildForBoth

Page 24: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Q&A

Page 25: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

Thank you!

Page 26: 04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.