W8 Unwrapping the not- so-obvious Kevin Dockx | 05/03/2013
Slide 2
2012 Microsoft Corporation. All rights reserved.
Slide 3
Slide 4
Agenda Navigation, Suspension Manager & complex types
Manipulating the navigation stack Handling exceptions in awaitable
methods that arent awaited Implementing a Variable Sized Grid
Incrementally loading data Debugging background tasks
Slide 5
Agenda Can you get local host access? Async & Await: theres
more to it Performance tips & tricks The most important tip of
this session Q&A
Slide 6
Slide 7
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, youll probably
want your own way to handle PLM
Slide 8
Suspension manager? Saves and restores the navigation state of
the Frame Crashes when you pass in a complex object private static
void SaveFrameNavigationState(Frame frame) { var frameState =
SessionStateForFrame(frame); frameState["Navigation"] =
frame.GetNavigationState(); }
Slide 9
Support complex objects? Simple approach: pass in simple types
If you must: (de)serialize yourself JsonHelper class
JsonHelper.ToJson when navigating JsonHelper.FromJson in
LoadState
Slide 10
Rgis Laurent Director of Operations, Global Knowledge
Competencies include: Gold Learning Silver System Management
Demo
Slide 11
Slide 12
Manipulating the Navigation Stack Why 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
Slide 13
Rgis Laurent Director of Operations, Global Knowledge
Competencies include: Gold Learning Silver System Management
Demo
Slide 14
Slide 15
Exceptions in Async methods Awaitable methods arent 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
Slide 16
Rgis Laurent Director of Operations, Global Knowledge
Competencies include: Gold Learning Silver System Management
Demo
Slide 17
Slide 18
Implementing a VariableSizedWrapGrid Create your own GridView
class, inherit from GridView Override
PrepareContainerForItemOverride Set RowSpan & ColumnSpan
VariableSizedWrapGrid.SetRowSpan
VariableSizedWrapGrid.SetColumnSpan
Slide 19
Implementing a VariableSizedWrapGrid In XAML, use the newly
created GridView Set the ItemPanelTemplate to a
VariableSizedWrapGrid Optionally define the orientation Optionally
define the maximum number of rows/columns Typically, define
ItemHeight & ItemWidth
Slide 20
Rgis Laurent Director of Operations, Global Knowledge
Competencies include: Gold Learning Silver System Management
Demo
Slide 21
Slide 22
Incrementally loading data An old trick Create a style for your
ListViewBase (ListView, GridView) Handle the ScrollViewers
ViewChanged event, check the offset value a better implementation
Bind to a collection that supports incremental loading Create a
collection, inherit from ObservableCollection Implement
ISupportIncrementalLoading
Slide 23
Rgis Laurent Director of Operations, Global Knowledge
Competencies include: Gold Learning Silver System Management
Demo
Slide 24
Slide 25
Debugging a Background Task Look for the Debug Location Toolbar
Its hidden by default!
Slide 26
Rgis Laurent Director of Operations, Global Knowledge
Competencies include: Gold Learning Silver System Management
Demo
Slide 27
Slide 28
Localhost access? No. And theres a good reason for that. Did I
say no? I meant yes. Due to a loopback rule, access to localhost is
restricted When youre in Visual Studio, your app is exempted from
this loopback rule => Allow Local Network loopback You can
manually exempt your app, using CheckNetIsolation.exe:
CheckNetIsolation.exe LoopbackExempt a p=AppID
Slide 29
Slide 30
Async & Await Support Cancellation Accept a Cancellation
Token in your method signature Check for cancellation in that
method Handle OperationCancelledException
Slide 31
Async & Await UI thread marshalling is what you get for
free Do you always want this? How can you avoid this? await
YourAsyncMethod().ConfigureAwait(false);
Slide 32
Async & Await Waiting for Any or All Tasks to complete var
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
Slide 33
Slide 34
Performance tip #1 Obvious, but maybe not so much: check
performance in Release Builds Writing to the output window slows
down your app tremendously Use the Performance Profiler
Slide 35
Peformance tip #2 Use the Async & Await tips Cancel running
operations when not needed Execute Tasks in Parallel when possible
Do not marshal to the UI thread when its not necessary
Slide 36
Performance tip #3 Reduce memory consumption Obvious? Yes, but
for an additional reason The 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
Slide 37
Performance tip #4 Learn about XAML parsing Do not load
resources that arent necessary Resource Dictionaries are fully
parsed, even though your page might only use one resource of it
Your start page shouldnt use application-wide dictionaries If you
use a resource throughout your app, put it in Application If you
dont, only reference that dictionary on the pages it is used, or
even in the page-specific resource dictionary
Slide 38
Performance tip #4 Optimize element count Dont write this: But
write this:
Slide 39 "> "> " title="Performance tip #4 Reuse brushes
Dont write this: Write this:
Performance tip #4 Reuse brushes Dont write this: Write
this: