Lessons learned from developing a Windows 8 Metro application in C#
description
Transcript of Lessons learned from developing a Windows 8 Metro application in C#
![Page 1: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/1.jpg)
Lessons learned from developing a Windows 8 Metro application in C#
Frode NilsenNilsen Labs
Ticki
![Page 2: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/2.jpg)
Agenda
Async-await done right Applying the MVVM pattern in Windows 8 Unscientific comparison of MS XAML technologies
![Page 3: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/3.jpg)
![Page 4: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/4.jpg)
The async-await pattern What it is What it not is Pitfalls Applying it properly
![Page 5: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/5.jpg)
Background
Feb 2012: Introduced in .Net 4.5 Spring 2011: Async CTP
June 2008: Task Parallel Library (TPL, .NET 4.0)
![Page 6: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/6.jpg)
The basics Two new c# keywords: async and await Makes asyncronous programming easy Makes asyncronous code clean
![Page 7: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/7.jpg)
The old way
![Page 8: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/8.jpg)
The new way
![Page 9: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/9.jpg)
The new way
12
![Page 10: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/10.jpg)
Defintion: Asynchrony
«Not at the same time»When method returns to the callerWhen the caller gets the return value
Threads
![Page 11: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/11.jpg)
Going deeper Async part 1, part 2 NDC 2012 presentation
Lucian Wischik (Microsoft Senior Program Manager) The Task Asynchronous Pattern (TAP) paper
by Stephen Toub Feb 2012 (Microsoft Async Guru) Progress reporting Cancellation Retrying Interleaving Throttling
![Page 12: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/12.jpg)
The power of asynchronous programming - example
![Page 13: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/13.jpg)
Option 1 – Serially
Rq 1 Rq 2 Rq 3
Time
Rq 4
![Page 14: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/14.jpg)
Option 2 – In Parallel
Rq 1
Rq 2
Rq 3
Time
Rq 4
![Page 15: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/15.jpg)
Request slot 2
Request slot 1
Option 3 – Throttled parallelism
Rq 1
Rq 2 Rq 3
Rq 4
Time
Google «AsyncSemaphore»
![Page 16: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/16.jpg)
DEMO – the powers of Tasks
![Page 17: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/17.jpg)
Async pitfalls 1 : Blocking threads
• Task.Wait()• Task.Result
![Page 18: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/18.jpg)
Async pitfalls 2 : Deadlocks
![Page 19: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/19.jpg)
Async pitfalls 3 : Exceptions
![Page 20: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/20.jpg)
Async pitfalls 4 : async void
![Page 21: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/21.jpg)
Rule of thumbs for async await
Never return async void from a method, unless it’s a UI event handler
Always await async methods.If you want to run several tasks in parallel, use await Task.WhenAll() or await Task.WhenAny()
Never use Task.Wait() or Task.Result to «synchronify» async methods, except in unit tests.
Never do async calls from constructors
![Page 22: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/22.jpg)
MVVM in Windows 8 Recap of the gist Applying it to Windows 8 Pitfalls Tools and tips
![Page 23: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/23.jpg)
MVVM Model
View
ViewModel
Send notificationsData binding
and commands
Send notifications
Updates
From MSDN
![Page 24: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/24.jpg)
MVVM Model
View
ViewModel
Send notificationsData binding
and commands
Send notifications
UpdatesDATABINDING
![Page 25: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/25.jpg)
Things that are impossible to do directly from the ViewModel
Give a visual element focus Select text in a text box Scroll an item into view Show / hide Charms, App-or Navigation-bar
![Page 26: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/26.jpg)
MVVM
“MVVM is targeted at modern UI development platforms which support Event-driven programming”
Quotes from the Wikipedia article
![Page 27: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/27.jpg)
MVVM
“MVVM is targeted at modern UI development platforms which support Event-driven programming”
Quotes from the Wikipedia article
![Page 28: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/28.jpg)
DEMO - events
![Page 29: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/29.jpg)
Controls and DataContext
A B C
A
ViewModels
Controls
B C D E
A ListView
![Page 30: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/30.jpg)
Controls and DataContext
A B C
A
ViewModels
Controls
B C D E
A ListView
![Page 31: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/31.jpg)
Controls and DataContext
A B C
A
ViewModels
Controls
B C D E
A ListView
D E
F
![Page 32: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/32.jpg)
Controls and DataContext
AB C
A
ViewModels
Controls
B C D E
A ListView
D E
F
![Page 33: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/33.jpg)
Solution: Pub/sub-pattern
Messenger
Publisher 1
Publisher 2
Publisher 2
Subscriber 1
Subscriber 2
Subscriber 2
![Page 34: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/34.jpg)
Solution: Pub/sub-pattern
Messenger
Publisher 1
Publisher 2
Publisher 2
Subscriber 1
Subscriber 2
Subscriber 2
User controlsViewModels
![Page 35: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/35.jpg)
Solution: Pub/sub-pattern
Messenger
Publisher 1
Publisher 2
Publisher 2
Subscriber 1
Subscriber 2
Subscriber 2MVVM Light Toolkit
link
![Page 36: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/36.jpg)
DEMO – pub/sub
![Page 37: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/37.jpg)
MVVM
“MVVM was designed to make use of data binding functions in WPF to better facilitate the separation of view layer development from the rest of the pattern by removing virtually all GUI code (“code-behind”) from the view layer”
Quotes from the Wikipedia article
![Page 38: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/38.jpg)
Things that are impossible to do directly from the ViewModel
Give a visual element focus Select text in a text box Scroll an item into view Show / hide Charms, App-or Navigation-bar
![Page 39: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/39.jpg)
Things I thought were impossible, but were not
Trigger animations Achieved through the use of databound visual states and
attached properties Alter the layout of a grid
You can databind Grid.ColumnDefinition.Width You can databind visibility of sections within the grid
![Page 40: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/40.jpg)
Windows 8 XAML vs «other XAML»
![Page 41: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/41.jpg)
Example 1: Bundled controls
![Page 42: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/42.jpg)
Example 1: Bundled controlsWinForm SL Win8
Button X X XDatePicker X XDataGrid X XTreeView X XComboBox X XTabControl X XGrid X XStackPanel X XMediaElement X X
![Page 43: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/43.jpg)
Making your own controls
Templates Visual tree vs Logical tree Custom dependency properties Custom events
![Page 44: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/44.jpg)
Example 2: Databinding
Binding update on property changed
![Page 45: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/45.jpg)
WinForms
![Page 46: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/46.jpg)
WPF
![Page 47: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/47.jpg)
Silverlight• No UpdateSourceTrigger
![Page 48: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/48.jpg)
WinRT• No UpdateSourceTrigger• No BindingOperations.GetBinding()
![Page 49: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/49.jpg)
WinRT 2 (a slightly better alternative)
link
![Page 50: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/50.jpg)
Microsoft XAML technologies WPF Silverlight Windows Phone 7 Windows 8 (WinRT)
Time
Qua
lity
![Page 52: Lessons learned from developing a Windows 8 Metro application in C#](https://reader031.fdocuments.in/reader031/viewer/2022020920/56816455550346895dd62136/html5/thumbnails/52.jpg)
Q & A