W8 – Unwrapping the not-so-obvious
1 / 45

W8 – Unwrapping the not-so-obvious - PowerPoint PPT Presentation

  • Uploaded on

W8 – Unwrapping the not-so-obvious. Kevin Dockx | 05/03/2013. #td2013fi. Hi! I’m Kevin. Technical Consultant @REALDOLMEN Web, XAML Wrote a few books as well @ KevinDockx [email protected] . http://www.mineforfacebook.com/. Agenda.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'W8 – Unwrapping the not-so-obvious' - noelle

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

W8 – Unwrapping the not-so-obvious

Kevin Dockx | 05/03/2013


Hi! I’m Kevin.

Technical Consultant @REALDOLMEN


Wrote a few books as well


[email protected]


Navigation, Suspension Manager & complex types

Manipulating the navigation stack

Handling exceptions in awaitable methods… that aren’t awaited

Implementing a Variable Sized Grid

Incrementally loading data

Debugging background tasks


Can you get local host access?

Async & Await: there’s more to it

Performance tips & tricks

The most important tip of this session


Suspension manager
Suspension manager? complex type when I navigate?

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

Suspension manager1
Suspension manager? complex type when I navigate?

Saves and restores the navigation state of the Frame

Crashes when you pass in a complex object

private static void SaveFrameNavigationState(Frame frame)


varframeState = SessionStateForFrame(frame);

frameState["Navigation"] = frame.GetNavigationState();


Support complex objects
Support complex objects? complex type when I navigate?

Simple approach: pass in simple types

If you must: (de)serialize yourself

JsonHelper class

JsonHelper.ToJson when navigating

JsonHelper.FromJson<T> in LoadState

Demo complex type when I navigate?


Manipulating the navigation stack
Manipulating the Navigation Stack 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

Demo stack?


Can you handle exceptions in async methods
Can stack?you handle Exceptions in async methods?

Exceptions in async methods
Exceptions in stack?Async methods

Awaitable 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

Demo stack?


How do you implement a variable sized grid
How stack?do you implement a Variable Sized Grid?

Implementing a variablesizedwrapgrid
Implementing a stack?VariableSizedWrapGrid

Create your own GridView class, inherit from GridView

Override PrepareContainerForItemOverride

Set RowSpan & ColumnSpan



Implementing a variablesizedwrapgrid1
Implementing a stack?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

Demo stack?


Can you incrementally load data
Can stack?you incrementally load data?

Incrementally loading data
Incrementally loading data stack?

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<T>

Implement ISupportIncrementalLoading

Demo stack?


Debugging a background task
Debugging a Background Task this Background Task?

Look for the Debug Location Toolbar

It’s hidden by default!

Demo this Background Task?


Localhost access
Localhost this Background Task? access?


And there’s a good reason for that.

Did I say no? I meant yes.

Due to a loopback rule, access to localhost is restricted

When you’re 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

Async await
Async this Background Task? & Await

Support Cancellation

Accept a Cancellation Token in your method signature

Check for cancellation in that method

Handle OperationCancelledException

Async await1
Async this Background Task? & Await

UI thread marshalling is what you get for free

Do you always want this?

How can you avoid this?

await YourAsyncMethod().ConfigureAwait(false);

Async await2
Async this Background Task? & Await

Waiting for Any or All Tasks to complete

varfirstCompleted = await Task.WhenAny(SomeAwaitableMethod1(),SomeAwaitableMethod2(),SomeAwaitableMethod3());

=> returns the first completed task result

varallCompleted = await Task.WhenAll(SomeAwaitableMethod1(),SomeAwaitableMethod2(),SomeAwaitableMethod3());

=> returns a list of all results of all tasks

Performance tip 1
Performance tip #1 this Background Task?

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

Peformance tip 2
Peformance this Background Task? 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 it’s not necessary

Performance tip 3
Performance tip #3 this Background Task?

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

Performance tip 4
Performance this Background Task?tip #4

Learn about XAML parsing

Do not load resources that aren’t necessary

Resource Dictionaries are fully parsed, even though your page might only use one resource of it

Your start page shouldn’t use application-wide dictionaries

If you use a resource throughout your app, put it in Application

If you don’t, only reference that dictionary on the pages it is used, or even in the page-specific resource dictionary

Performance tip 41
Performance tip #4 this Background Task?

Optimize element count

Don’t write this:


<Rectangle Fill="Black"/>


But write this:

<Grid Background=“Black” />

Performance tip 42
Performance tip #4 this Background Task?

Reuse brushes

Don’t write this:


<Button Content="Submit"><Button.Foreground>



Write this:

<TextBoxForeground="{StaticResourceTextColor}" />

<Button Content="Submit" Foreground=“

{StaticResourceTextColor}" />

Performance tip 43
Performance tip #4 this Background Task?

Minimize redrawing to the same place on the screen

Don’t write this

<Grid Background="Black">


<RowDefinition Height="*"/>

<RowDefinition Height="*"/>


<Rectangle Grid.Row="1" Fill="White" Opacity=".5"/>


Performance tip 44
Performance tip #4 this Background Task?

But write this:



<RowDefinition Height="*"/>

<RowDefinition Height="*"/>


<Rectangle Grid.Row="1" Fill="Black"/>

<Rectangle Grid.Row="1" Fill="#FF7F7F7F"/>


There’s more: http://msdn.microsoft.com/en-us/library/windows/apps/hh994641.aspx

Hire a designer. this Background Task?

Q&A this Background Task?

Example this Background Task?

Thank you for coming!

Feedback can be given via mobile or laptop through techdays.fi seminar schedule.