31
Don´t do more "copy and paste" between Windows Store and Windows Phone Apps Sara Silva http://netpont o.org 35th Meeting in Lisbon - 26/01/2013

Do not do more copy and paste between windows store and windows phone apps

Embed Size (px)

DESCRIPTION

See more information in pcl.codeplex.com

Citation preview

Page 1: Do not do more copy and paste between windows store and windows phone apps

Don´t do more "copy and paste" between Windows Store and Windows Phone

AppsSara Silva

http://netponto.org35th Meeting in Lisbon - 26/01/2013

Page 2: Do not do more copy and paste between windows store and windows phone apps

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

Page 3: Do not do more copy and paste between windows store and windows phone apps

Agenda

• Introdution

• Sharing code files

• Portable Class Library

• Portable Class Library + MVVM

• Libraries

• Conclusion

Page 4: Do not do more copy and paste between windows store and windows phone apps

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

Page 5: Do not do more copy and paste between windows store and windows phone apps

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.

Page 6: Do not do more copy and paste between windows store and windows phone apps

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.”

Page 7: Do not do more copy and paste between windows store and windows phone apps

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

Page 8: Do not do more copy and paste between windows store and windows phone apps

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

Page 9: Do not do more copy and paste between windows store and windows phone apps

Share code filesExample, MVVM Light toolkit :

Page 10: Do not do more copy and paste between windows store and windows phone apps

“Add as Link”

demonstration

Page 11: Do not do more copy and paste between windows store and windows phone apps

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

Page 12: Do not do more copy and paste between windows store and windows phone apps

Portable Class LibraryFeatures supported on various platforms *:

* .Net Framework greater or equal to 4.0.3

Page 13: Do not do more copy and paste between windows store and windows phone apps

Portable Class LibraryThe documentation on MSDN announces support:

Page 14: Do not do more copy and paste between windows store and windows phone apps

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

Page 15: Do not do more copy and paste between windows store and windows phone apps

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!”

Page 16: Do not do more copy and paste between windows store and windows phone apps

Portable Class Library Project

demonstration

Page 17: Do not do more copy and paste between windows store and windows phone apps

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" />

Page 18: Do not do more copy and paste between windows store and windows phone apps

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.

Page 19: Do not do more copy and paste between windows store and windows phone apps

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

Page 20: Do not do more copy and paste between windows store and windows phone apps

Consuming Odata Service

demonstration

Page 21: Do not do more copy and paste between windows store and windows phone apps

Architecture

*only external portable libraries

Page 22: Do not do more copy and paste between windows store and windows phone apps

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

Page 23: Do not do more copy and paste between windows store and windows phone apps

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!

Page 24: Do not do more copy and paste between windows store and windows phone apps

Q & A

Page 25: Do not do more copy and paste between windows store and windows phone apps

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

Page 26: Do not do more copy and paste between windows store and windows phone apps

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++)

Page 27: Do not do more copy and paste between windows store and windows phone apps

Sponsors “GOLD”

Twitter: @PTMicrosoft http://www.microsoft.com/portugal

Page 29: Do not do more copy and paste between windows store and windows phone apps

Sponsors “Bronze”

Page 30: Do not do more copy and paste between windows store and windows phone apps

Upcoming meetings

• 23/02/2013 – February (Lisbon)16/03/2013 – March (Lisbon)

Reserve these days! :)

Page 31: Do not do more copy and paste between windows store and windows phone apps

Thanks!

Sara [email protected]

http://www.saramgsilva.comhttp://twitter.com/saramgsilvahttp://pt.linkedin.com/in/saramgsilvahttp://www.facebook.com/saramgsilva