Download presentation
Presentation is loading. Please wait.
Published byGladys Johnson Modified over 9 years ago
1
Lessons learned from developing a Windows 8 Metro application in C# Frode Nilsen Nilsen Labs Ticki
2
Agenda Async-await done right Applying the MVVM pattern in Windows 8 Unscientific comparison of MS XAML technologies
4
The async-await pattern What it is What it not is Pitfalls Applying it properly
5
Background Feb 2012: Introduced in.Net 4.5 Spring 2011: Async CTP June 2008: Task Parallel Library (TPL,.NET 4.0)
6
The basics Two new c# keywords: async and await Makes asyncronous programming easy Makes asyncronous code clean
7
The old way
8
The new way
9
1 2
10
Defintion: Asynchrony «Not at the same time» When method returns to the caller When the caller gets the return value Threads
11
Going deeper Async part 1, part 2 NDC 2012 presentation Lucian Wischik (Microsoft Senior Program Manager)part 1part 2 The Task Asynchronous Pattern (TAP) paper by Stephen Toub Feb 2012 (Microsoft Async Guru)Task Asynchronous Pattern Progress reporting Cancellation Retrying Interleaving Throttling
12
The power of asynchronous programming - example
13
Option 1 – Serially Rq 1Rq 2Rq 3 Time Rq 4
14
Option 2 – In Parallel Rq 1 Rq 2 Rq 3 Time Rq 4
15
Request slot 2 Request slot 1 Option 3 – Throttled parallelism Rq 1 Rq 2Rq 3 Rq 4 Time Google «AsyncSemaphore»AsyncSemaphore
16
DEMO – the powers of Tasks
17
Async pitfalls 1 : Blocking threads Task.Wait() Task.Result
18
Async pitfalls 2 : Deadlocks
19
Async pitfalls 3 : Exceptions
20
Async pitfalls 4 : async void
21
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
22
MVVM in Windows 8 Recap of the gist Applying it to Windows 8 Pitfalls Tools and tips
23
MVVM Model View ViewModel Send notifications Data binding and commands Send notifications Updates From MSDN
24
MVVM Model View ViewModel Send notifications Data binding and commands Send notifications Updates DATABINDING
25
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
26
MVVM “MVVM is targeted at modern UI development platforms which support Event-driven programming” Quotes from the Wikipedia article
27
MVVM “MVVM is targeted at modern UI development platforms which support Event-driven programming” Quotes from the Wikipedia article
28
DEMO - events
29
Controls and DataContext A B C A ViewModels Controls B C D E A ListView
30
Controls and DataContext A B C A ViewModels Controls B C D E A ListView
31
Controls and DataContext A B C A ViewModels Controls B C D E A ListView D E F
32
Controls and DataContext A B C A ViewModels Controls B C D E A ListView D E F
33
Solution: Pub/sub-pattern Messenger Publisher 1 Publisher 2 Subscriber 1 Subscriber 2
34
Solution: Pub/sub-pattern Messenger Publisher 1 Publisher 2 Subscriber 1 Subscriber 2 User controls ViewModels
35
Solution: Pub/sub-pattern Messenger Publisher 1 Publisher 2 Subscriber 1 Subscriber 2 MVVM Light Toolkit link
36
DEMO – pub/sub
37
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
38
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
39
Things I thought were impossible, but were not Trigger animations Achieved through the use of databound visual states and attached propertiesdatabound visual states attached properties Alter the layout of a grid You can databind Grid.ColumnDefinition.Width You can databind visibility of sections within the grid
40
Windows 8 XAML vs «other XAML»
41
Example 1: Bundled controls
42
WinFormSLWin8 ButtonXXX DatePickerXX DataGridXX TreeViewXX ComboBoxXX TabControlXX GridXX StackPanelXX MediaElementXX
43
Making your own controls Templates Templates Visual tree vs Logical tree Visual tree vs Logical tree Custom dependency properties Custom dependency properties Custom events
44
Example 2: Databinding Binding update on property changed
45
WinForms
46
WPF
47
Silverlight No UpdateSourceTrigger
48
WinRT No UpdateSourceTrigger No BindingOperations.GetBinding()
49
WinRT 2 (a slightly better alternative) link
50
Microsoft XAML technologies WPF Silverlight Windows Phone 7 Windows 8 (WinRT) Time Quality
51
Thank you Mail: nilsenlabs@gmail.com Twitter: @nilzor Blog: www.nilzorblog.com
52
Q & A
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.