W8 – Unwrapping the not-so-obvious
description
Transcript of W8 – Unwrapping the not-so-obvious
![Page 1: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/1.jpg)
W8 – Unwrapping the not-so-obviousKevin Dockx | 05/03/2013
#td2013fi
![Page 2: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/2.jpg)
©2012 Microsoft Corporation. All rights reserved.
Hi! I’m Kevin.
Technical Consultant @REALDOLMENWeb, XAMLWrote a few books as well
![Page 3: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/3.jpg)
http://www.mineforfacebook.com/
![Page 4: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/4.jpg)
AgendaNavigation, Suspension Manager & complex typesManipulating the navigation stackHandling exceptions in awaitable methods… that aren’t awaitedImplementing a Variable Sized GridIncrementally loading dataDebugging background tasks
![Page 5: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/5.jpg)
AgendaCan you get local host access?Async & Await: there’s more to itPerformance tips & tricksThe most important tip of this sessionQ&A
![Page 6: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/6.jpg)
Why is the Suspension Manager crashing when I pass in a complex type when I navigate?
![Page 7: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/7.jpg)
Suspension manager?Implementation included in Store Apps
Allows you to save & restore state of a View
Makes it easy to support PLM for simple apps, but for larger apps, you’ll probably want your own way to handle PLM
![Page 8: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/8.jpg)
Suspension manager?Saves and restores the navigation state of the Frame
Crashes when you pass in a complex objectprivate static void SaveFrameNavigationState(Frame frame){ var frameState = SessionStateForFrame(frame); frameState["Navigation"] = frame.GetNavigationState();}
![Page 9: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/9.jpg)
Support complex objects?Simple approach: pass in simple types
If you must: (de)serialize yourselfJsonHelper classJsonHelper.ToJson when navigatingJsonHelper.FromJson<T> in LoadState
![Page 10: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/10.jpg)
RégisLaurentDirector of Operations, Global KnowledgeCompetencies include:Gold LearningSilver System Management
Demo
#td2013fi
![Page 11: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/11.jpg)
While we’re at it… can we manipulate the navigation stack?
![Page 12: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/12.jpg)
Manipulating the Navigation StackWhy would you want to do this?Typically: showing a disclaimer, an initial wizard, …
Check for value in Application Settings to see if page has to be shown
Use Frame.SetNavigationState(“1,0,0”) to remove all pages from the stack
![Page 13: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/13.jpg)
RégisLaurentDirector of Operations, Global KnowledgeCompetencies include:Gold LearningSilver System Management
Demo
#td2013fi
![Page 14: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/14.jpg)
Can you handle Exceptions in async methods?
![Page 15: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/15.jpg)
Exceptions in Async methodsAwaitable methods aren’t always awaited
In such a case, when Exception happens => NOT caught by App_UnhandledException
What if you still want to know about it? Handle the TaskScheduler.UnobservedTaskException event
![Page 16: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/16.jpg)
RégisLaurentDirector of Operations, Global KnowledgeCompetencies include:Gold LearningSilver System Management
Demo
#td2013fi
![Page 17: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/17.jpg)
How do you implement a Variable Sized Grid?
![Page 18: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/18.jpg)
Implementing a VariableSizedWrapGridCreate your own GridView class, inherit from GridView
Override PrepareContainerForItemOverride
Set RowSpan & ColumnSpanVariableSizedWrapGrid.SetRowSpanVariableSizedWrapGrid.SetColumnSpan
![Page 19: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/19.jpg)
Implementing a VariableSizedWrapGridIn XAML, use the newly created GridView
Set the ItemPanelTemplate to a VariableSizedWrapGridOptionally define the orientationOptionally define the maximum number of rows/columnsTypically, define ItemHeight & ItemWidth
![Page 20: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/20.jpg)
RégisLaurentDirector of Operations, Global KnowledgeCompetencies include:Gold LearningSilver System Management
Demo
#td2013fi
![Page 21: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/21.jpg)
Can you incrementally load data?
![Page 22: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/22.jpg)
Incrementally loading dataAn old trick…Create a style for your ListViewBase (ListView, GridView)Handle the ScrollViewers’ ViewChanged event, check the offset value
… a better implementationBind to a collection that supports incremental loadingCreate a collection, inherit from ObservableCollection<T>Implement ISupportIncrementalLoading
![Page 23: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/23.jpg)
RégisLaurentDirector of Operations, Global KnowledgeCompetencies include:Gold LearningSilver System Management
Demo
#td2013fi
![Page 24: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/24.jpg)
Do I really have to sit and wait for 15 minutes to debug this Background Task?
![Page 25: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/25.jpg)
Debugging a Background TaskLook for the Debug Location ToolbarIt’s hidden by default!
![Page 26: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/26.jpg)
RégisLaurentDirector of Operations, Global KnowledgeCompetencies include:Gold LearningSilver System Management
Demo
#td2013fi
![Page 27: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/27.jpg)
Can I access my local host from a Windows Store app?
![Page 28: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/28.jpg)
Localhost access?No.And there’s a good reason for that.
Did I say no? I meant yes.Due to a loopback rule, access to localhost is restrictedWhen you’re in Visual Studio, your app is exempted from this loopback rule => Allow Local Network loopbackYou can manually exempt your app, using CheckNetIsolation.exe:CheckNetIsolation.exe LoopbackExempt –a –p=AppID
![Page 29: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/29.jpg)
Async & Await: there’s more to it
![Page 30: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/30.jpg)
Async & AwaitSupport CancellationAccept a Cancellation Token in your method signatureCheck for cancellation in that methodHandle OperationCancelledException
![Page 31: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/31.jpg)
Async & AwaitUI thread marshalling is what you get for freeDo you always want this?How can you avoid this?
await YourAsyncMethod().ConfigureAwait(false);
![Page 32: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/32.jpg)
Async & AwaitWaiting for Any or All Tasks to completevar firstCompleted = await Task.WhenAny(SomeAwaitableMethod1(),SomeAwaitableMethod2(),SomeAwaitableMethod3());=> returns the first completed task result
var allCompleted = await Task.WhenAll(SomeAwaitableMethod1(),SomeAwaitableMethod2(),SomeAwaitableMethod3());=> returns a list of all results of all tasks
![Page 33: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/33.jpg)
Help! My app has performance issues!
![Page 34: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/34.jpg)
Performance tip #1Obvious, but maybe not so much: check performance in Release BuildsWriting to the output window slows down your app tremendously
Use the Performance Profiler
![Page 35: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/35.jpg)
Peformance tip #2Use the Async & Await tipsCancel running operations when not neededExecute Tasks in Parallel when possibleDo not marshal to the UI thread when it’s not necessary
![Page 36: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/36.jpg)
Performance tip #3Reduce memory consumptionObvious? Yes, but for an additional reasonThe less memory your app uses, the more likely it is to stay in memory instead of being terminated, ensuring a faster restart after being suspended
![Page 37: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/37.jpg)
Performance tip #4Learn about XAML parsing
Do not load resources that aren’t necessaryResource Dictionaries are fully parsed, even though your page might only use one resource of itYour start page shouldn’t use application-wide dictionariesIf you use a resource throughout your app, put it in ApplicationIf you don’t, only reference that dictionary on the pages it is used, or even in the page-specific resource dictionary
![Page 38: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/38.jpg)
Performance tip #4Optimize element countDon’t write this:
<Grid> <Rectangle Fill="Black"/>
</Grid>
But write this: <Grid Background=“Black” />
![Page 39: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/39.jpg)
Performance tip #4Reuse brushesDon’t write this:
<TextBox><TextBox.Foreground><SolidColorBrush Color="#FF3F42CC"/></TextBox.Foreground></TextBox> <Button Content="Submit"><Button.Foreground>
<SolidColorBrush Color="#FF3F42CC"/> </Button.Foreground></Button>
Write this:<TextBox Foreground="{StaticResource
TextColor}" /><Button Content="Submit" Foreground=“{StaticResource TextColor}" />
![Page 40: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/40.jpg)
Performance tip #4Minimize redrawing to the same place on the screenDon’t write this
<Grid Background="Black"> <Grid.RowDefinitions>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>
</Grid.RowDefinitions> <Rectangle Grid.Row="1" Fill="White" Opacity=".5"/> </Grid>
![Page 41: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/41.jpg)
Performance tip #4But write this:
<Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Rectangle Grid.Row="1" Fill="Black"/> <Rectangle Grid.Row="1" Fill="#FF7F7F7F"/></Grid>
There’s more: http://msdn.microsoft.com/en-us/library/windows/apps/hh994641.aspx
![Page 42: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/42.jpg)
The most important tip of this session
![Page 43: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/43.jpg)
RégisLaurentDirector of Operations, Global KnowledgeCompetencies include:Gold LearningSilver System Management
Hire a designer.
![Page 44: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/44.jpg)
t Q&A
![Page 45: W8 – Unwrapping the not-so-obvious](https://reader036.fdocuments.net/reader036/viewer/2022081507/56816134550346895dd08d4b/html5/thumbnails/45.jpg)
© 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentations. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Thank you for coming!Feedback can be given via mobile or laptop through techdays.fi seminar schedule.
Example
#td2013fi