KEMBAR78
Wpug mvvm and data binding | PPTX
@slodge




  Cross Platform Mobile
C#, Mvvm and DataBinding
          WP7
         Android
         iPhone
           iPad
          Win 8
@slodge



@slodge
@slodge



http://bit.ly/mvvmcross
@slodge



                Coming up…
•   C# Cross Platform
•   An Mvvm Introduction
•   Some Code: TwitterSearch
•   Some Extras
•   Q&A
@slodge




Mobile
 C#
@slodge



                   Why C#?
• You already have:
   – skills
   – tools
   – code
• “low level”
   – compiles
   – testable
   – quick
• Enables native look and feel on all platforms
• Generics, Linq, Async and the future…
@slodge



         What you need – WP7:


•   A Windows 7 PC
•   Free Tools or better!
•   AppHub account $99
•   A phone sometimes helps…
@slodge



        What you need - Android:


•   Windows PC or a Mac
•   Free Tools
•   Play account $25
•   A phone really helps…
•   Mono 4 Android $400
@slodge



           What you need - iOS:


•   An Intel-based Mac
•   Account-Linked Tools
•   iOS Account $99
•   A phone often helps…
•   MonoTouch - $400
@slodge



Getting Started Links…
@slodge




MVVM
@slodge



                Mvvm Basics


           get/set
           Properties




                           ViewModel
           call Commands




                            ViewModel




                                                            Model
View




                              ViewModel
                                          Whatever C# you




                                                             Model
 View




                                          like!




                                                              Model
   View




          notify changes
          some messaging
@slodge



         MVVM Technical Details
•   Properties
•   INotifyPropertyChanged
•   INotifyCollectionChanged
•   Data Binding
•   IValueConverter
•   ICommand
•   UI Thread
@slodge



               Mvvm Benefits
• The “normal” reasons:
  – Separation of concerns
  – Designability (Blendability)
  – Testability


• The MvvmCross reason:
  – Cross platform code reuse
@slodge



            Mvvm Cross Platform

                get/set
                Properties




                                ViewModel
                call Commands




                                 ViewModel




                                                                  Model
  View




                                   ViewModel
                                                Whatever C# you




                                                                   Model
   View




                                                like!




                                                                    Model
     View




               notify changes
               some messaging




Platform Specific                              Shared
@slodge



Some longer introductions…
@slodge




Twitter
Search
@slodge



         What we want to build…

Home Page:            Result Page:
@slodge



The Model and Service
@slodge



HomeViewModel
@slodge



TwitterViewModel
@slodge



               Navigation
• The HomeViewModel calls:




• Which means the TwitterViewModel gets
  created with:
@slodge




Test it!
@slodge



Some Unit Tests
@slodge




“Win32”
@slodge



A Console App
@slodge




WP7
@slodge



A WP7 App
@slodge



           Inside WP7 App…




• No C#, just XAML with {Binding}
• Binding syntax includes:
        {Binding Path, Converter=Converter,
      ConverterParameter=Parameter, Mode=Mode,
              FallbackValue=Fallback}
@slodge



           Inside WP7 App…
• ViewModel is auto-wired via
 MvxPhonePage<TViewModel> inheritance
@slodge



           Inside WP7 App…
• ValueConverters are used for presenting
  property values
@slodge



           Inside WP7 App…
• Lists…
@slodge



            Inside WP7 App…
• Navigation is mapped to XAML Uri’s – with
  parameters put in a JSON object in the path.
• Implementation hidden from the View and
  ViewModel.
@slodge




Android
@slodge



An Android App
@slodge



         Inside Android App…
• No C#, just xml
  with MvxBind
  attributes
• MvxBind syntax is
  JSON based on
  XAML structure.
@slodge



       Binding – Xaml versus Mvx
WP7:
 Text=‚{Binding SearchText,Mode=TwoWay}‛


Android:
         local:MvxBind=‚{‘Text’:
 {‘Path’:’SearchText’,’Mode’:’TwoWay’}}‛
@slodge



       Binding – Xaml versus Mvx
WP7:
              Text=‚{Binding
   Timestamp,Converter={StaticResource
                TimeAgo}}‛

Android:
         local:MvxBind=‚{‘Text’:
{‘Path’:’Timestamp’,Converter’:’TimeAgo’}
                    }‛
@slodge



         Inside Android App…
• ValueConverters are used for presenting
  property values
@slodge



          Inside Android App
• ViewModels are auto-wired using
 MvxBindingActivityView<TViewModel>
 inheritance
@slodge



           Inside Android App…
• Lists…
@slodge



          Inside Android App…
• Navigation is mapped to Intent’s– with
  parameters put in a JSON object in the Extras.
• Implementation hidden from the View and
  ViewModel.
@slodge




iPhone
@slodge



iPhone App
@slodge



           Inside iPhone App
• UI is drawn in XML using Xcode
• XIB is linked to C# classes using MonoTouch
• Binding is declared in C# using JSON
@slodge
@slodge



           Inside iPhone App
• ViewModels are auto-wired using inheritance
  and a constructor parameter:
@slodge



           Inside iPhone App
• ValueConverters are used for presenting
  property values
@slodge



           Inside iPhone App
• Lists…
@slodge



           Inside iPhone App
• Navigation is mapped to C# calls.
• Implementation mostly hidden from the View
  and ViewModel
@slodge
@slodge




iPad
@slodge



iPad App
@slodge



              Inside iPad App
• The iPad App is actually the iPhone App.
• But the UI is different – take advantage of the
  larger screen.
• Achieved in code: IMvxTouchViewPresenter
@slodge
@slodge




Windows 8 (WinRT/Metro)
@slodge



Windows 8 Metro App
@slodge



Inside Windows 8 Metro App…
@slodge



Inside Windows 8 Metro App…
@slodge



   Inside Windows 8 Metro App…
It’s like WP7….
• No C#, just XAML with {Binding}


• ValueConverters are used in Bindings
• Lists are filled using ItemsSource

• BUT: Navigation is achieved through code – not
  Xaml Uri’s
@slodge



     Inside Windows 8 Metro App
Warnings:
• MvvmCross WinRT implementation
  is “first cut”
   – e.g. differences in ViewModel lifecycle
• Under-the-hood Win8
  implementation is native code –
  there are big differences.
   – differences in XAML
   – differences in .Net/C#
• Everything is async.
@slodge



   Inside Windows 8 Metro App…
• Beyond the warnings – look at the numbers!
@slodge




Recap
@slodge



        TwitterSearch recap…
• Shared C# Core
  – ViewModels
  – Models
  – Services
  – Converters
  – Tests

• UIs are “mostly XML/JSON”
@slodge




Some
Extras
@slodge



     Panoramas, Pivots and Tabs
• Tabbed navigation is another common
  metaphor across phones and across ipads too.
• Generally speaking think of each “tab” as its
  own ViewModel.
• Binding is then available in each platform.
@slodge



“Tabs”
@slodge



                 Jump Lists
• Are possible…
• But need separate list
  formats on each
  platform…
• Thinking about how
  best to do this…
• Open to ideas!
@slodge



          Dependency Injection
• Useful for testing
• Essential for injecting platform specific services –
  e.g:
   – Camera capture
   – GPS services
   – NFC
• MvvmCross wraps OpenNetCF DI – adds some
  extension methods:
   – RegisterServiceInstance<TService>(instance)
   – RegisterServiceType<TService, TServiceType>()
   – GetService<TService>()
@slodge



                Native Types
• Sometimes #define is necessary…
• Personally, I try to use interfaces and DI
  wherever possible.
@slodge



                       i18n
• Choices:
  – Each platform has their own localisation format
    available.
  – Xamarin are supporting .resx files…
  – MvvmCross provides a .json file mechanism
  – Or write your own
@slodge



               Blendability
• Limited support available right now.
• It’s relatively easy to integrate your own
  design time ViewModelLocator with design
  time data…
@slodge



                 Problems?
• WinRT – issues…
• Xcode designer - OMG
• iOS development and debugging is most
  painful
• Some compiler differences – JIT
• Multiple “core” project files annoying – and
  breaks refactoring
• MvvmCross still changing
@slodge




 That’s
all folks
@slodge



             That’s all folks…
Hopefully we covered:
• C# Cross Platform
• An Mvvm Introduction
• Some Code: TwitterSearch
• Some Extras

There might be time for:
• Q&A
@slodge



@slodge
@slodge



http://bit.ly/mvvmcross
@slodge



Alternatives: http://bit.ly/rrnLDT

Wpug mvvm and data binding

  • 1.
    @slodge CrossPlatform Mobile C#, Mvvm and DataBinding WP7 Android iPhone iPad Win 8
  • 2.
  • 3.
  • 4.
    @slodge Coming up… • C# Cross Platform • An Mvvm Introduction • Some Code: TwitterSearch • Some Extras • Q&A
  • 5.
  • 6.
    @slodge Why C#? • You already have: – skills – tools – code • “low level” – compiles – testable – quick • Enables native look and feel on all platforms • Generics, Linq, Async and the future…
  • 7.
    @slodge What you need – WP7: • A Windows 7 PC • Free Tools or better! • AppHub account $99 • A phone sometimes helps…
  • 8.
    @slodge What you need - Android: • Windows PC or a Mac • Free Tools • Play account $25 • A phone really helps… • Mono 4 Android $400
  • 9.
    @slodge What you need - iOS: • An Intel-based Mac • Account-Linked Tools • iOS Account $99 • A phone often helps… • MonoTouch - $400
  • 10.
  • 11.
  • 12.
    @slodge Mvvm Basics get/set Properties ViewModel call Commands ViewModel Model View ViewModel Whatever C# you Model View like! Model View notify changes some messaging
  • 13.
    @slodge MVVM Technical Details • Properties • INotifyPropertyChanged • INotifyCollectionChanged • Data Binding • IValueConverter • ICommand • UI Thread
  • 14.
    @slodge Mvvm Benefits • The “normal” reasons: – Separation of concerns – Designability (Blendability) – Testability • The MvvmCross reason: – Cross platform code reuse
  • 15.
    @slodge Mvvm Cross Platform get/set Properties ViewModel call Commands ViewModel Model View ViewModel Whatever C# you Model View like! Model View notify changes some messaging Platform Specific Shared
  • 16.
  • 17.
  • 18.
    @slodge What we want to build… Home Page: Result Page:
  • 19.
  • 20.
  • 21.
  • 22.
    @slodge Navigation • The HomeViewModel calls: • Which means the TwitterViewModel gets created with:
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
    @slodge Inside WP7 App… • No C#, just XAML with {Binding} • Binding syntax includes: {Binding Path, Converter=Converter, ConverterParameter=Parameter, Mode=Mode, FallbackValue=Fallback}
  • 30.
    @slodge Inside WP7 App… • ViewModel is auto-wired via MvxPhonePage<TViewModel> inheritance
  • 31.
    @slodge Inside WP7 App… • ValueConverters are used for presenting property values
  • 32.
    @slodge Inside WP7 App… • Lists…
  • 33.
    @slodge Inside WP7 App… • Navigation is mapped to XAML Uri’s – with parameters put in a JSON object in the path. • Implementation hidden from the View and ViewModel.
  • 34.
  • 35.
  • 36.
    @slodge Inside Android App… • No C#, just xml with MvxBind attributes • MvxBind syntax is JSON based on XAML structure.
  • 37.
    @slodge Binding – Xaml versus Mvx WP7: Text=‚{Binding SearchText,Mode=TwoWay}‛ Android: local:MvxBind=‚{‘Text’: {‘Path’:’SearchText’,’Mode’:’TwoWay’}}‛
  • 38.
    @slodge Binding – Xaml versus Mvx WP7: Text=‚{Binding Timestamp,Converter={StaticResource TimeAgo}}‛ Android: local:MvxBind=‚{‘Text’: {‘Path’:’Timestamp’,Converter’:’TimeAgo’} }‛
  • 39.
    @slodge Inside Android App… • ValueConverters are used for presenting property values
  • 40.
    @slodge Inside Android App • ViewModels are auto-wired using MvxBindingActivityView<TViewModel> inheritance
  • 41.
    @slodge Inside Android App… • Lists…
  • 42.
    @slodge Inside Android App… • Navigation is mapped to Intent’s– with parameters put in a JSON object in the Extras. • Implementation hidden from the View and ViewModel.
  • 43.
  • 44.
  • 45.
    @slodge Inside iPhone App • UI is drawn in XML using Xcode • XIB is linked to C# classes using MonoTouch • Binding is declared in C# using JSON
  • 46.
  • 47.
    @slodge Inside iPhone App • ViewModels are auto-wired using inheritance and a constructor parameter:
  • 48.
    @slodge Inside iPhone App • ValueConverters are used for presenting property values
  • 49.
    @slodge Inside iPhone App • Lists…
  • 50.
    @slodge Inside iPhone App • Navigation is mapped to C# calls. • Implementation mostly hidden from the View and ViewModel
  • 51.
  • 52.
  • 53.
  • 54.
    @slodge Inside iPad App • The iPad App is actually the iPhone App. • But the UI is different – take advantage of the larger screen. • Achieved in code: IMvxTouchViewPresenter
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
    @slodge Inside Windows 8 Metro App… It’s like WP7…. • No C#, just XAML with {Binding} • ValueConverters are used in Bindings • Lists are filled using ItemsSource • BUT: Navigation is achieved through code – not Xaml Uri’s
  • 61.
    @slodge Inside Windows 8 Metro App Warnings: • MvvmCross WinRT implementation is “first cut” – e.g. differences in ViewModel lifecycle • Under-the-hood Win8 implementation is native code – there are big differences. – differences in XAML – differences in .Net/C# • Everything is async.
  • 62.
    @slodge Inside Windows 8 Metro App… • Beyond the warnings – look at the numbers!
  • 63.
  • 64.
    @slodge TwitterSearch recap… • Shared C# Core – ViewModels – Models – Services – Converters – Tests • UIs are “mostly XML/JSON”
  • 65.
  • 66.
    @slodge Panoramas, Pivots and Tabs • Tabbed navigation is another common metaphor across phones and across ipads too. • Generally speaking think of each “tab” as its own ViewModel. • Binding is then available in each platform.
  • 67.
  • 68.
    @slodge Jump Lists • Are possible… • But need separate list formats on each platform… • Thinking about how best to do this… • Open to ideas!
  • 69.
    @slodge Dependency Injection • Useful for testing • Essential for injecting platform specific services – e.g: – Camera capture – GPS services – NFC • MvvmCross wraps OpenNetCF DI – adds some extension methods: – RegisterServiceInstance<TService>(instance) – RegisterServiceType<TService, TServiceType>() – GetService<TService>()
  • 70.
    @slodge Native Types • Sometimes #define is necessary… • Personally, I try to use interfaces and DI wherever possible.
  • 71.
    @slodge i18n • Choices: – Each platform has their own localisation format available. – Xamarin are supporting .resx files… – MvvmCross provides a .json file mechanism – Or write your own
  • 72.
    @slodge Blendability • Limited support available right now. • It’s relatively easy to integrate your own design time ViewModelLocator with design time data…
  • 73.
    @slodge Problems? • WinRT – issues… • Xcode designer - OMG • iOS development and debugging is most painful • Some compiler differences – JIT • Multiple “core” project files annoying – and breaks refactoring • MvvmCross still changing
  • 74.
  • 75.
    @slodge That’s all folks… Hopefully we covered: • C# Cross Platform • An Mvvm Introduction • Some Code: TwitterSearch • Some Extras There might be time for: • Q&A
  • 76.
  • 77.
  • 78.