Windows Phone MVVM and Unit Testing Step by Step Andy Wigley Windows Phone Development MVP, Mobile Software Consultant APPA Mundi Ltd WPH208.

Slides:



Advertisements
Similar presentations
Evaluations Submit your evals online.
Advertisements

Expression Blend 4 – deep dive
Understanding the MVVM pattern
Patterns & practices Symposium 2013 Tips for building a Windows Store app using XAML and C#: The Kona project Francis Cheung
Developing HTML5 Application using MVVM pattern Pekka Ylenius.
USING THE MODEL-VIEW- VIEWMODEL PATTERN Laurent Bugnion Director, User Experience Integration IdentityMine
An Introduction to the Model-View-ViewModel Pattern Srsly? Another MV* Pattern? Srsly? Another MV* Pattern?
WinRT Apps
Laurent Bugnion Director, UX MVVM Powers Silverlight Development.
Real world Windows Phone development Igor
Evaluations Submit your evals online.
App Compat for Nerds: Understanding, Troubleshooting, and Fixing Busted Apps chris jackson principal consultant microsoft corporation WCL402.
Windows Phone 8 device and app management Alan Meeus Sr. Technical Product Manager Windows Phone Division Microsoft Corporation WPH205.
Khalid Siddiqui Senior Architect, Mobility Center of Excellence Microsoft Corporation SESSION CODE: WPH311.
Todd Brix Senior Director, Windows Phone Marketplace Microsoft Corporation WPH206.
Building Metro style UIs Paul Gusmorino Lead Program Manager Microsoft Corporation DEV354.
.NET Database Technologies: Introduction to WPF and Entity Framework DataBinding.
Building Metro style apps with HTML and JavaScript Paul Gusmorino Lead Program Manager Microsoft Corporation.
An Introduction to Silverlight Matt Harrington Developer Evangelist, Microsoft October 20, 2011.
Building Metro style apps with HTML and JavaScript Paul Gusmorino Lead Program Manager Microsoft Corporation.
Windows Phone 8 Tips & Tricks for Developers Sascha Corti, Microsoft Switzerland Technical Evangelist | techpreacher.corti.com.
Adam Calderon – C# MVP Application Development Practice Lead Interknowlogy.
Getting Started with Caliburn.Micro and Windows Phone 7 Gary Ewan Park Twitter: Blog:
Building Windows 8 Apps for the Enterprise Robert Green Technical Evangelist Microsoft Corporation DEV358.
Advanced Microsoft SharePoint 2010 Upgrade Troubleshooting Todd Klindt SharePoint Nerd Rackspace OSP339.
Deep Dive on SharePoint Ribbon Development and Extensibility Chris O’Brien SharePoint MVP Independent OSP433.
Building SharePoint Online Applications in a Hybrid World Chris Johnson General Manager Provoke Solutions - Seattle OSP331.
Visual C#/Visual Basic: Becoming a Guru with Existing Features Peter Ritchie Principle Peter Ritchie Inc. Software Consulting DEV325.
Creating LOB Metro style Apps in XAML Using Blend and Visual Studio Jeffrey Ferman Program Manager Microsoft Corporation DEV369.
Windows Phone: Building Enterprise Apps Rob Tiffany Architect Microsoft Corporation WPH207.
Paul D. Sheriff DEV216 MVVM Made Easy Paul D. Sheriff
Windows Phone: Collaborate Through Exchange, SharePoint, Lync and Office 365 Augusto Valdez Senior Product Manager – Windows Phone Microsoft Corporation.
Nikhil Kothari Software Architect Microsoft Corporation Session Code: WUX312.
Windows 10 UWP MVVM In Depth
DEV321. demo Rule: Any slide about UX must be charcoal gray or black.
Application Lifecycle Management Tools for C++ in Visual Studio 2012 Rong Lu Program Manager Visual C++ Microsoft Corporation DEV316.
A Lap Around Windows Presentation Foundation (WPF) 4.5 Pete Brown | XAML and Gadget Guy Microsoft Corporation DEV335.
04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga
V 1.0 Programming III. Automatic notifications with data binding (…Changed, INofityPropertyChanged, ObservableCollection, DataTemplate) Data formatters.
What’s New in.NET 4.5 Layla Driscoll Senior Program Manager Microsoft Corporation.
Model View ViewModel Architecture. MVVM Architecture components.
Maarten Struys Windows Phone Evangelist Alten PTS SESSION CODE: WPH303.
Rob Tiffany Mobility Architect Microsoft Corporation SESSION CODE: WPH306.
Windows 8 apps and the MVVM pattern SILVERLIGHTSHOW.NET WEBINARS SERIES GILL CLEEREN, October 16 th
Programming with MVVM Miguel A. Castro Architect -
Andy Wigley Device Application Development MVP APPA Mundi Ltd SESSION CODE: WEM309.
/DEV/TM #1 Building Cross-Platform Apps with Xamarin and MvvmCross Flavius-Radu DEMIAN.
Async Made Simple in Windows 8, with C# and Visual Basic Alex Turner Program Manager VB/C# Compilers Microsoft Corporation DEV332.
The cutting edge event for ITPros and Devs December 7-8, 2013 Athens, Greece Fix it once use it everywhere Elias Markelis MCT, Windows Phone Enthusiast.
Going Beyond F11: Debug Better and Faster with Visual Studio 2012 Brian A. Randell Senior Consultant MCW Technologies DEV317.
Building Metro style apps with XAML with.NET Tim Heuer Program Manager Microsoft Corporation DEV353.
In Windows 8 Store Applications
Real world Windows Phone development
Ben Riga 02 | Basics of View Models Ben Riga
TechEd /26/2018 3:42 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Build Windows 10 UWP MVVM Apps with Prism
It’s a Knockout! MVVM Style Web Development
Miguel A. Castro Architect IDesign
Integrating Security Roles into Microsoft Silverlight Applications
An introduction to MVVM using WPF NISCHAL S
Tech Ed North America /1/2018 4:27 PM Required Slide
Real World Developer Testing
1/10/2019 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Page Navigation and Data Binding in Windows Runtime Apps
From Development to Production: Optimizing for Continuous Delivery
XAML Deep Dive for Windows & Windows Phone Apps Jump Start
Architecting Silverlight Applications with MVVM
Bringing existing managed code into Metro style apps
Tech Ed North America /6/2019 2:07 PM Required Slide
An Introduction to the Model-View-ViewModel Pattern
Presentation transcript:

Windows Phone MVVM and Unit Testing Step by Step Andy Wigley Windows Phone Development MVP, Mobile Software Consultant APPA Mundi Ltd WPH208

WHAT IS UNIT TESTING?

demo Windows Phone Project that is NOT testable

MVVM GOODNESS The Benefits of Separation of Concerns

Data Business Logic Presentation ViewModel Model View ViewModel View ViewModel View

Windows Phone Properties of controls can be bound to a public property of a data object – In the example above, the Text property of the TextBlock is bound to the LineThree property of some data source Define the data source by setting: – The DataContext property of any containing FrameworkElement-derived class (a containing control, the page, or the frame), or – The ItemsSource property of a List control

Windows Phone The Mode property determines how changes are synchronized between the target control and data source – OneTime – Control property is set once to the data value and any subsequent changes are ignored – OneWay – Changes in the data object are synchronized to the control property, but changes in the control are not synchronized back to the data object – TwoWay – Changes in the data object are synchronized to the control property and vice-versa

Windows Phone ViewModels implement INotifyPropertyChanged public class ItemViewModel : INotifyPropertyChanged { private string lineOne; public string LineOne { get { return lineOne; } set { if (value != lineOne) { lineOne = value; NotifyPropertyChanged("LineOne"); } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String propertyName) { if (null != PropertyChanged) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }

demo MVVM and DataBinding

View.xaml.csViewModelView.xaml Events Event Handlers

<Button Content="Press this" Height="72" Margin="90,464,0,0" Name="button1" Width="300" Command="{Binding HelloCommand}"/> <i:InvokeCommandAction Command="{Binding SelectionChanged}" CommandParameter="{Binding ElementName=listBox1, Path=SelectedIndex}"/>

public class MainViewModel : ViewModelBase {... // Note that RelayCommand is NOT in the Silverlight class libraries. This RelayCommand object // comes from the MVVMLight framework. You could create your own implementation – it must implement // the Silverlight ICommand interface. private RelayCommand _myHelloCommand; /// /// Gets the HelloCommand. /// public RelayCommand HelloCommand { get { return _myHelloCommand ?? (_myHelloCommand = new RelayCommand( () => { this.WelcomeTitle = "You changed the Title!"; })); }

Frame ViewModel1 View1.xaml View2.xaml Navigate GoBack ViewModel2 NavigationService o

NavigationService Implementation public class NavigationService : INavigationService { public void NavigateTo(Uri pageUri) { var _mainFrame = Application.Current.RootVisual as PhoneApplicationFrame; _mainFrame.Navigate(pageUri); } public void GoBack() { var _mainFrame = Application.Current.RootVisual as PhoneApplicationFrame; if (_mainFrame.CanGoBack) { _mainFrame.GoBack(); }

demo MVVM and Commanding

DEPENDENCY INJECTION Creating Testable Objects by Removing Dependencies on Other Objects

DataService public class MainViewModel { private DataService dataSvc; public MainViewModel() { dataSvc= new DataService(); } public void XYZmethod() { var theCar = dataSvc.Car;... NavigationService.Instance.GoBack(); }... } DataService NavigationService

public class MainViewModel { private IDataService dataSvc; private INavigationService navSvc; public MainViewModel(IDataService data, INavigationService nav) { dataSvc= data; navSvc = nav; } public void XYZmethod() { var theCar = dataSvc.Car;... navSvc.GoBack(); }... } DataService : IDataService NavigationService : INavigationService MockDataService : IDataService MockNavigationService : INavigationService

Dependency Injection Container public class ViewModelLocator { static ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); // Using SimpleIOC // Register Services if (ViewModelBase.IsInDesignModeStatic) SimpleIoc.Default.Register (); else SimpleIoc.Default.Register (); // Register ViewModels SimpleIoc.Default.Register (); } // Following property returns an instance of MainViewModel, with dependencies injected public MainViewModel Main { get { return ServiceLocator.Current.GetInstance (); } }

demo Dependency Injection

UNIT TESTING WINDOWS PHONE XAML APPLICATIONS Using the Silverlight Windows Phone Unit Testing Framework

Unit Test Example /// /// Tests that the Initialize method takes a copy of the 'main' car /// NOTE uses the Mock Datastore to test the VM in isolation /// [TestMethod] public void InitializeCopiesCar() { var mDS = new MockDataService(); var carDetailsVM = new CarDetailsViewModel(mDS, new MockNavigationService()); var origName = mDS.Car.Name; var origOdo = mDS.Car.InitialOdometerReading; // Call method to test carDetailsVM.Initialize(); Assert.AreEqual(origName, carDetailsVM.Car.Name, "Name of car not copied"); Assert.AreEqual(origOdo, carDetailsVM.Car.InitialOdometerReading, “Odo not copied"); }

demo Unit Testing

Blog: Cheat Sheet for Unit Testing on Windows Phone 7 Microsoft patterns & practices: Building Testable Windows Phone Apps Microsoft patterns & practices: Developing a Windows Phone App using the MVVM Pattern Windows Phone 7 Continuous Integration Testing Framework

MVVMLight Caliburn Micro Simple MVVM Toolkit Catel nRoute UltraLight.mvvm

WPH207 Windows Phone: Building Enterprise Apps AAP401 Real World Developer Testing with Visual Studio 2012 Windows Phone for Developers – TLC – Hall 1 Find Me Later At TLC After This Session 11:45 – 12:45

Tuesday 12:00G105WPH201What's New 14:45G105WPH203Build Apps and Games for WP :30E107WPH202Collaborate Through Exchange, SharePoint, Lync and Office 365 Wednesday 12:00E104WPH204Application UI Design Principles 14:45D201WPH304Security Deep Dive 17:00G105WPH206How to Make Money with your Applications and Games Thursday 08:30G105WPH205Device and App Management 12:00G105WPH301Tiles and Notifications 14:45G105WPH207Building Enterprise Apps 16:30D201WPH302Localization and Globalization Friday 08:30G105WPH303Windows Phone: Optimizing Application Performance 10:15G105WPH208Windows Phone: MVVM and Unit Testing Step by Step

Connect. Share. Discuss. Learning Microsoft Certification & Training Resources TechNet Resources for IT Professionals Resources for Developers

Evaluations Submit your evals online