Don´t do more "copy and paste" between Windows Store and Windows Phone
AppsSara Silva
http://netponto.org35th Meeting in Lisbon - 26/01/2013
Sara SilvaWindows 8 & Windows Phone Developer
Communities :
MCPD: Windows Developer 3.5 MCTS: WPF 3.5/4.0 and Windows Forms 3.5 Math Degree: Specialization in Computing - DMUC
Agenda
• Introdution
• Sharing code files
• Portable Class Library
• Portable Class Library + MVVM
• Libraries
• Conclusion
Introdution
2 Platforms
3 applications
6 projectsPílula WP Pílula Win8Galinho WP Galinho Win824 WP 24 Win8
Nota: Same version for WP7.5 and WP8
Introdution• Repeated code between projects => 2 times the same task.• Bug in WP => bug in Win8 and vice versa => 2 times the same correction.
• Less features.• Less time for others taks.• Cost are double:
“If the cost for the company is Y €/hour => 2*Y€/hour”“If the cost is sleep time => Less time sleeping ”
• Demotivation for developers.
Share code files
• Use “Add as Link” to include same files in multiple projects.
• Use directives for platform specifications.
• Use partial class and partial methods.
• Change once, change everywhere.
“Reusing code by including code files in multiple projects.”
Share code files
#if !NETFX_CORE
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
#else
public object Convert(object value, Type targetType, object parameter, string culture)
#endif
Using directives:
• NETFX_CORE Windows 8• WINDOWS_PHONE Windows Phone
It can “generate” code unreadable
Share code filesPartial Classes and partial methods
• Shared functionality in one code file.
• Platform specific code in additional code file.
• Classes are marked as partial and compiled into a single class.
• Separates platform specific features.
• Can use partial methods as a mechanism to separate out platform specific logic.
DataSource.cs | DataSource.WP8.cs | DataSource.Win8.cs
Share code filesExample, MVVM Light toolkit :
“Add as Link”
demonstration
Portable Class Library
More help for developing cross-platform applications using. Net Framework, since it allows you to create "portable assemblies" that can be referenced in projects for multiple platforms without the need to make changes.
Will contain the lowest common denominator types:• .NET Framework
• Silverlight
• .NET for Windows Store apps,
• Windows Phone
• Xbox 360
Portable Class LibraryFeatures supported on various platforms *:
* .Net Framework greater or equal to 4.0.3
Portable Class LibraryThe documentation on MSDN announces support:
Portable Class Library
When create a project
Visual Studio 2010 (Extention +SP1) and 2012 provides a Portable Class Library project template.
“targets” definitions:
Editing the target in a project
Portable Class LibraryVisual Studio 2010 (Extenstion +SP1) and 2012 provides a Portable Class Library project template.
Result:
“ One Source One Project One Binary
Multiple Platforms!”
Portable Class Library Project
demonstration
Portable Class Library• Not contains types related to UI because the UI behavior differs between the
different platforms;
• Windows 8 is required for create Windows Store Apps, but is not required for create Portable Class Libraries that supports Windows Store Apps.
• Silverlight-based application must ensure that the minimum runtime version required for the application is set to version 4.0.60129.0 or later
<param name="minRuntimeVersion" value="4.0.60129.0" />
Portable Class Library + MVVMViews (Platform-specific)
How to display informationWritten in XAML
View Models (Portable)What information to displayFlow of interaction
Models (Portable)Data objectsBusiness logicEtc.
Portable Class Library + MVVMAvailable classes to help MVVM implementation:
System.Collections.ObjectModel• ObservableCollection<T> class• ReadOnlyObservableCollection<T> class
System.Collections.Specialized• INotifyCollectionChanged class• NotifyCollectionChangedAction class• NotifyCollectionChangedEventArgs class• NotifyCollectionChangedEventHandler class
System.ComponentModel• DataErrorsChangedEventArgs class• INotifyDataErrorInfo class• INotifyPropertyChanged class• System.Windows.Input.ICommand class• All classes in the System.ComponentModel.DataAnnotations namespace
Consuming Odata Service
demonstration
Architecture
*only external portable libraries
Libraries• Microsoft.Bcl.Async
Allow to use async/await without .Net Framework 4.5
• Portable Toolkit for MVVM Fork from toolkit MVVM Light: Portable.MvvmLightLibs
• MvvmCross Library for MVVM in Windows Phone, Windows Store, iOS, and Android
• Portable Class Libraries Contrib Portable adapters and APIs
• Json.NET JSON framework
Conclusion• Separate UI from app logic using the Model-View-ViewModel pattern.
• Share portable .NET code in Portable Class Library
• “Abstactration pattern is the key”
• Dependency injection allow to encapsulate platform specifications.
• Use common Windows Runtime API (Add as Link)
• If necessary, extend shared classes to add specific functionality of the platform.
• When we want to share code, designing the architecture should bear this in mind!
Q & A
References• Cross-Platform Development with the .NET Framework
• Using Portable Class Library with Model-View-View Model
• Create Cross-platform Apps using Portable Class Libraries
• Building Apps for Windows Phone 8 Jump Start (MVA)
• Visual Studio Toolbox:Portable Class Libraries
• Create a Continuous Client Using Portable Class Libraries
• How to Leverage your Code across WP8 and Windows 8
• Portable Library Articles series by Jeremy Likness
• How to Make Portable Class Libraries Work for You
• Portable Class Library Enlightenment / Adaptation
• Portable Class Libraries – Net Framework 4.0
• Portable Class Libraries – Net Framework 4.5
Others Samples• PortableNotepad - This sample shows how to access non-portable functionality from portable code by creating a portable abstraction
with platform-specific implementations.Goals: Abstraction for Load / Save data in filesProjects: Windows Phone, Windows Store App, WPF App
• ContosoHelpdesk – This demo show advanced concepts.
Goals: Navigation, inversion of control, and synchronizing data across multiple clients with Windows Azure Service Bus.Projects: Windows Phone, Windows Store App Note: Use Autofac portable library (IoC Container)
• TwitterSearch – This sample shows how to create a twitter search client.
Goals: Twitter Search API, Azure Mobile Services,Projects: Windows Phone, Windows Store App, Console AppNote: Use MVVMCross portable library
• Disentanglement - This is a solver for Thinkfun's Gordian's Knot puzzle which runs on Windows and Windows Phone. It uses a
Portable Class Library to share the solver code between the different platformsGoals: Share the solver code.Projects: Windows Phone App, Desktop App, Xbox App
• PixPresenter - is a photo sharing app that enables you to connect to another device using Proximity (NFC) and a tap gesture, and then
send pictures back and forth between the connected devices. It demonstrates code sharing techniques that you can use when developing an app for botGoals: Windows Runtime API common codeProjects: Windows Phone, Windows Store App (C# and C++)
Sponsors “GOLD”
Twitter: @PTMicrosoft http://www.microsoft.com/portugal
Sponsors “Bronze”
Upcoming meetings
• 23/02/2013 – February (Lisbon)16/03/2013 – March (Lisbon)
Reserve these days! :)
Thanks!
Sara [email protected]
http://www.saramgsilva.comhttp://twitter.com/saramgsilvahttp://pt.linkedin.com/in/saramgsilvahttp://www.facebook.com/saramgsilva