1 / 52

Lessons learned from developing a Windows 8 Metro application in C#

Ticki. Lessons learned from developing a Windows 8 Metro application in C#. Frode Nilsen Nilsen Labs. 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

coye
Download Presentation

Lessons learned from developing a Windows 8 Metro application in C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ticki Lessons learned from developing a Windows 8 Metro application in C# Frode NilsenNilsen Labs

  2. Agenda • Async-await done right • Applying the MVVM pattern in Windows 8 • Unscientific comparison of MS XAML technologies

  3. The async-await pattern • What it is • What it not is • Pitfalls • Applying it properly

  4. Background • Feb 2012: Introduced in .Net 4.5 • Spring 2011: Async CTP • June 2008: Task Parallel Library (TPL, .NET 4.0)

  5. The basics • Two new c# keywords: async and await • Makes asyncronous programming easy • Makes asyncronous code clean

  6. The old way

  7. The new way

  8. The new way 1 2

  9. Defintion: Asynchrony • «Not at the same time» • When method returns to the caller • When the caller gets the return value Threads

  10. 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

  11. The power of asynchronous programming - example

  12. Option 1 – Serially Rq 1 Rq 2 Rq 3 Rq 4 Time

  13. Option 2 – In Parallel Rq 1 Rq 2 Rq 3 Rq 4 Time

  14. Option 3 – Throttled parallelism • Google «AsyncSemaphore» Request slot 1 Rq 2 Rq 3 Request slot 2 Rq 1 Rq 4 Time

  15. DEMO – the powers of Tasks

  16. Async pitfalls 1 : Blocking threads • Task.Wait() • Task.Result

  17. Async pitfalls 2 : Deadlocks

  18. Async pitfalls 3 : Exceptions

  19. Async pitfalls 4 : async void

  20. 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

  21. MVVM in Windows 8 • Recap of the gist • Applying it to Windows 8 • Pitfalls • Tools and tips

  22. MVVM Model From MSDN Send notifications Data binding and commands ViewModel Send notifications Updates View

  23. MVVM Model Send notifications Data binding and commands DATABINDING ViewModel Send notifications Updates View

  24. 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

  25. MVVM Quotes from the Wikipedia article “MVVM is targeted at modern UI development platforms which support Event-driven programming”

  26. MVVM Quotes from the Wikipedia article “MVVM is targeted at modern UI development platforms which support Event-drivenprogramming”

  27. DEMO - events

  28. ViewModels Controls and DataContext Controls A ListView A C B C E A B D

  29. ViewModels Controls and DataContext Controls A ListView A C B C E A B D

  30. ViewModels Controls and DataContext Controls A ListView A C D B E C E A B D F

  31. ViewModels Controls and DataContext Controls A ListView C D B E A C E A B D F

  32. Solution: Pub/sub-pattern Publisher 1 Subscriber 1 Messenger Publisher 2 Subscriber 2 Publisher 2 Subscriber 2

  33. Solution: Pub/sub-pattern Publisher 1 Subscriber 1 Messenger Publisher 2 Subscriber 2 Publisher 2 Subscriber 2 ViewModels User controls

  34. Solution: Pub/sub-pattern Publisher 1 Subscriber 1 Messenger Publisher 2 Subscriber 2 Publisher 2 Subscriber 2 MVVM Light Toolkit link

  35. DEMO – pub/sub

  36. MVVM Quotes from the Wikipedia article “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”

  37. 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

  38. 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

  39. Windows 8 XAML vs «other XAML»

  40. Example 1: Bundled controls

  41. Example 1: Bundled controls

  42. Making your own controls • Templates • Visual tree vs Logical tree • Custom dependency properties • Custom events

  43. Example 2: Databinding • Binding update on property changed

  44. WinForms

  45. WPF

  46. Silverlight • No UpdateSourceTrigger

  47. WinRT • No UpdateSourceTrigger • No BindingOperations.GetBinding()

  48. WinRT 2 (a slightly better alternative) link

  49. Microsoft XAML technologies • WPF • Silverlight • Windows Phone 7 • Windows 8 (WinRT) Quality Time

More Related