Load the Data for a Window Asynchronously After It Has Rendered

August 11, 2009

Suppose you need to show a window or user control that loads and displays a lot of data. If you initiate the loading of the data in either the constructor for the control/window or its Loaded event handler, it won’t draw or render correctly to the screen until the data has finished loading. This makes the window load up very ungracefully and slowly, showing first of all the outline of the window, and then filling in the middle later.

In this situation you can use the Dispatcher to ensure that the data is only loaded after the window has finished loading and rendering correctly.

Read the rest of this entry »

Execute a Method Asynchronously Using the Dispatcher Queue

July 28, 2009

The following example demonstrates a simple application with a window containing a Button and a TextBlock. When the user clicks the Button, there is code in the code-behind that counts upward from 3, searching for prime numbers. When the program finds a prime number, it updates the TextBlock with the number. The user can click the Button again to stop the search.

The example schedules a prime number check on the window’s Dispatcher property. It uses a DispatcherPriority of SystemIdle, which ensures that any pending UI events are processed in between each check.

Read the rest of this entry »

How to show a design-time value for a binding in Silverlight, like WPF’s FallbackValue

July 16, 2009

In WPF you can use the FallbackValue property of a Binding to specify a value to use when the binding is unable to return a value. This can happen for any of these reasons:

  • It’s in design mode – E.g. in Expression Blend.
  • The path to the binding cannot be resolved
  • The value converter, if there is one, cannot convert the resulting value.
  • The resulting value is not a valid value for the target property.

This is really useful in Expression Blend as you can use the FallbackValue to display a value at design time, so you get to see a realistic view of the UI as you’re designing it. Otherwise it’s very difficult to style data bound controls.

As the FallbackValue property isn’t available in Silverlight, I wrote a simple AttachedProperty to get the same effect:

<TextBox 
    Text="{Binding LastName, Mode=TwoWay}" 
    Design:DesignTime.Text="Last name" />

Setting a value for the DesignTime.Text property will display it whilst working in Blend.

The code for the attached property is below.

Read the rest of this entry »

Tips and Tricks with the Dispatcher and DispatcherPriority in WPF

July 15, 2009

It wasn’t until I’d used WPF for about a year that I really started to appreciate the value of using the Dispatcher to execute chunks of code asynchronously with a specific DispatcherPriority. Now I would say I use this a couple of times a day and find it invaluable.

In this series of posts, I’m going to introduce the Dispatcher object and DispatcherPriority enum, and outline some of the useful ways to leverage them.

In this first post I’ll introduce the Dispatcher and DispatcherPriority, and then I’ll follow it up with posts showing some uses for them:

  1. Execute a Method Asynchronously Using the Dispatcher Queue
  2. Load the Data for a Window Asynchronously After It Has Rendered
  3. Load the Items in a ListBox Asynchronously
  4. Implement Application.DoEvents in WPF
  5. BackgroundCommand (and demo)
  6. Silverlight – combo box
  7. Dual Targeting – DispatcherHelper

 

Introducing the Dispatcher and DispatcherPriority

 

If you need to execute a method asynchronously without blocking the UI thread, you can invoke the method on the Dispatcher property of a UI element, and specify a System.Windows.Threading.DispatcherPriority that’s lower than the Render priority.

Read the rest of this entry »

Introduction

July 1, 2009

Hello, I’m Sam – I’m a WPF/Surface/Silverlight developer and I work in Dubai as Principal Consultant at Infusion Development. Finally getting round to starting a blog. I hope to use it to post useful code snippets and links I use myself and need to remember, and hopefully start to contribute back to the WPF/Silverlight community a bit.

I’ve been developing for over 10 years, and using .NET and then WPF since their early betas. I’m a big exponent of design patterns in application architecture in general, and the Model-View-ViewModel presentation pattern in particular. I’ve been using this pattern for over 3 years in WPF development, and the related Presentation Model in WinForms development before that. I hope to share some of my code and techniques on this blog with anyone who’s interested.

I plan to post a fairly in-depth series of posts on MVVM and how I approach it over the coming weeks and months, but just to get me going I will post up some of my little snippets of techniques I’ve picked up along the way.


Design a site like this with WordPress.com
Get started