Eric grover strategies for sharing code with windows 8 and windows phone 8 apps

30
Strategi es For Sharing Code With Windows Store & Windows Phone Apps Presented by Eric Grover Senior Consultant with

description

Strategies for sharing code with windows 8 and windows phone 8 apps

Transcript of Eric grover strategies for sharing code with windows 8 and windows phone 8 apps

Page 1: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Strategi es For Sharing Code With Windows Store

& Windows Phone AppsPresented by Eric Grover

Senior Consultant with

Page 2: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Why Build for Windows 8?

• Up to 80% revenue sharing.

• 200 markets worldwide.

• 100 million Windows 8 licenses sold in the first 6 months. 250 million apps downloaded.

• Use your existing development skills.

Page 3: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Why Build for Windows Phone 8?• 128 markets worldwide.

• Has replaced BlackBerry as #3 consumer phone OS.

• Is quickly becoming the Enterprise replacement for B-Berry / BES.

• Crossed double digit market share in Europe (passing the fruity phone in several markets).

• Use your existing development skills.

• Room in the app store for high quality apps.

Page 4: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

What Do They Share?

Page 5: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Design Differences

• Different form factors.

• Different controls. Win8 - Gridview (horizontal), WinPhone -LongListSelector (vertical).

• Different back buttons (hardware vs. software)

• Different application button bars.

• Different live tiles.

Page 6: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Where Do We Start?

•We need a pattern that allows us to separate the UI layer from the application state/logic and from the data layer.

Page 7: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Model-View-View Model Pattern

• The M-V-VM pattern is based on a variation of the MVC pattern which promotes a separation of concerns between UI and application logic layers.

• Two-way data binding allows the use of the Observer pattern to de-couple Views from the state and operations of the application.

Page 8: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Where Should We Put Shared Code?

•We need a place to put our shared View Models and Models so that it can be referenced by either platform.

Page 9: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Portable Class Libraries (PCL)

The Portable Class Library project supports a subset of assemblies from the .NET Framework, Silverlight, .NET for Windows Store apps, Windows Phone, and Xbox 360, and now Xamarin iOS/Android!

Models View Models

Windows StoreViews, and platform

specific code

Windows Phone 8Views, and platform

specific code

Xamarin iOS or Android Views, and

platform specific code

Page 10: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Portable Class Libraries (PCL)

What kind of code goes into a PCL?

• Models

• Core View Models

• Data Layer / Service Consumption

• Other Generic Logic

Page 11: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Portable Class Libraries (PCL)

Page 12: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

What About Other Types Of Code?

• Not everything can be moved back into the PCL.

• Some code, like converters, has to stay in our UI projects.

Page 13: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Linked Files

Linked files are shortcuts added to one project that points to a file in another project.

What types of files should I consider linking?

• Converters

• Common platform implementation

• Assets

Page 14: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Linked Files

Page 15: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

What About Code That is Slightly Different?

• Sometimes, there are only slight differences between platforms and their API (i.e. IValueConverter).

Page 16: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Compiler Directives

Compiler directives tell the compiler which block of code to run based on the platform being compiled for.

• #if NETFX_CORE/WINDOWS_PHONE• Allows you to use linked files when there are minor

differences because of platform divergence.

Page 17: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Compiler Directives

Page 18: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

PCL vs. Linked FilesPCL

Pros:

• Cleaner, simpler code.

Cons:

• Limited API access.

• Can’t reference non-PCL assemblies.

Linked Files

Pros:

• Full API access.

Cons:

• More complex (messy) code.

Page 19: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Copy & Paste (no, really)

• XAML snippets

• Styles

Page 20: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

What If I Need To Call Platform Specific API Or Non PCL assembly in My View

Model / Commands?

• Sometimes Your View Model will need to trigger navigation, read or write from local storage, or pin a tile to the start screen.

Page 21: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

OOP Patterns

• Use base classes with virtual methods that are overridden with platform specific API calls in derived classes.

• Use interfaces to de-couple derived classes so that more code can reside in PCL.

Page 22: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Platform Adapter• A Platform Adapter is an abstract class for which each

platform project implements a specific set or interfaces for their platform.

• Code in the PCL can then be written to invoke platform specific tasks (such as navigation, file I/O, etc.) agnostic to the implementation for each platform.

Page 23: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Platform Adapter

Page 24: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

What about Localization?

• Windows Phone 8 and Windows Store apps use different approaches, making resource re-use challenging.

Page 25: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Localization

• Windows Phone 8 apps use .resx files, localization through data binding.

<TextBlock Text="{Binding Loc.AppTitle, Source={StaticResource Localization}}"/>

Page 26: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Localization

• Windows Store apps use .resw files, localization through x:UId.

<TextBlock x:Uid="TextBlock1" Text="DesignTimeText"/>

Page 27: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Localization• Storing localization string in PCL is possible, but

you must:

• Hack your PCL project file to add the <Supported Cultures> element.

• Move all of your .resx files from your Phone project into the PCL.

• Keep stub .resw files in your Windows 8 project with at least one string in each file.

• Not use the new x:Uid attribute in Windows Store apps. You must use the legacy data binding approach instead.

Page 28: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Localization

Page 29: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Summary• PCLs let us share:• Models• Business Logic• Web Services• Helpers• Core View Models• Core Commands

• Linked Files• Converters• Some Controls• Common API implementation• Static assets

Page 30: Eric grover   strategies for sharing code with windows 8 and windows phone 8 apps

Questions?