Eric grover strategies for sharing code with windows 8 and windows phone 8 apps
-
Upload
eric-grover -
Category
Technology
-
view
1.164 -
download
1
description
Transcript of 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
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.
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.
What Do They Share?
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.
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.
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.
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.
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
Portable Class Libraries (PCL)
What kind of code goes into a PCL?
• Models
• Core View Models
• Data Layer / Service Consumption
• Other Generic Logic
Portable Class Libraries (PCL)
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.
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
Linked Files
What About Code That is Slightly Different?
• Sometimes, there are only slight differences between platforms and their API (i.e. IValueConverter).
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.
Compiler Directives
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.
Copy & Paste (no, really)
• XAML snippets
• Styles
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.
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.
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.
Platform Adapter
What about Localization?
• Windows Phone 8 and Windows Store apps use different approaches, making resource re-use challenging.
Localization
• Windows Phone 8 apps use .resx files, localization through data binding.
<TextBlock Text="{Binding Loc.AppTitle, Source={StaticResource Localization}}"/>
Localization
• Windows Store apps use .resw files, localization through x:UId.
<TextBlock x:Uid="TextBlock1" Text="DesignTimeText"/>
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.
Localization
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
Questions?