Upload
gladys-johnson
View
216
Download
1
Embed Size (px)
Citation preview
Lessons learned from developing a Windows 8 Metro application in C#
Frode NilsenNilsen Labs
Ticki
Agenda
Async-await done right
Applying the MVVM pattern in Windows 8
Unscientific comparison of MS XAML technologies
The async-await pattern
What it is
What it not is
Pitfalls
Applying it properly
Background
Feb 2012: Introduced in .Net 4.5 Spring 2011: Async CTP
June 2008: Task Parallel Library (TPL, .NET 4.0)
The basics
Two new c# keywords: async and await
Makes asyncronous programming easy
Makes asyncronous code clean
The old way
The new way
The new way
1
2
Defintion: Asynchrony
«Not at the same time»
When method returns to the caller
When the caller gets the return value
Threads
Going deeper
Async part 1, part 2 NDC 2012 presentation Lucian Wischik (Microsoft Senior Program Manager)
The Task Asynchronous Pattern (TAP) paperby Stephen Toub Feb 2012 (Microsoft Async Guru) Progress reporting
Cancellation
Retrying
Interleaving
Throttling
The power of asynchronous programming - example
Option 1 – Serially
Rq 1 Rq 2 Rq 3
Time
Rq 4
Option 2 – In Parallel
Rq 1
Rq 2
Rq 3
Time
Rq 4
Request slot 2
Request slot 1
Option 3 – Throttled parallelism
Rq 1
Rq 2 Rq 3
Rq 4
Time
Google «AsyncSemaphore»
DEMO – the powers of Tasks
Async pitfalls 1 : Blocking threads
• Task.Wait()• Task.Result
Async pitfalls 2 : Deadlocks
Async pitfalls 3 : Exceptions
Async pitfalls 4 : async void
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
MVVM in Windows 8
Recap of the gist
Applying it to Windows 8
Pitfalls
Tools and tips
MVVM Model
View
ViewModel
Send notificationsData binding
and commands
Send notifications
Updates
From MSDN
MVVM Model
View
ViewModel
Send notificationsData binding
and commands
Send notifications
UpdatesDATABINDING
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
MVVM
“MVVM is targeted at modern UI development platforms which support Event-driven programming”
Quotes from the Wikipedia article
MVVM
“MVVM is targeted at modern UI development platforms which support Event-driven programming”
Quotes from the Wikipedia article
DEMO - events
Controls and DataContext
A B C
A
ViewModels
Controls
B C D E
A ListView
Controls and DataContext
A B C
A
ViewModels
Controls
B C D E
A ListView
Controls and DataContext
A B C
A
ViewModels
Controls
B C D E
A ListView
D E
F
Controls and DataContext
AB C
A
ViewModels
Controls
B C D E
A ListView
D E
F
Solution: Pub/sub-pattern
Messenger
Publisher 1
Publisher 2
Publisher 2
Subscriber 1
Subscriber 2
Subscriber 2
Solution: Pub/sub-pattern
Messenger
Publisher 1
Publisher 2
Publisher 2
Subscriber 1
Subscriber 2
Subscriber 2
User controlsViewModels
Solution: Pub/sub-pattern
Messenger
Publisher 1
Publisher 2
Publisher 2
Subscriber 1
Subscriber 2
Subscriber 2MVVM Light Toolkit
link
DEMO – pub/sub
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
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
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
Windows 8 XAML vs «other XAML»
Example 1: Bundled controls
Example 1: Bundled controls
WinForm SL Win8
Button X X X
DatePicker X X
DataGrid X X
TreeView X X
ComboBox X X
TabControl X X
Grid X X
StackPanel X X
MediaElement X X
Making your own controls
Templates
Visual tree vs Logical tree
Custom dependency properties
Custom events
Example 2: Databinding
Binding update on property changed
WinForms
WPF
Silverlight
• No UpdateSourceTrigger
WinRT
• No UpdateSourceTrigger
• No BindingOperations.GetBinding()
WinRT 2 (a slightly better alternative)
link
Microsoft XAML technologies
WPF
Silverlight
Windows Phone 7
Windows 8 (WinRT)
Time
Qu
ality
Q & A