Creating Custom Content Loaders

Out of the box, the navigation framework simply loads XAML files from your project or from a referenced assembly into a Frame control. However, perhaps you want to add some custom logic to this process, such as dynamically downloading XAPs from the server or checking that the users are authorized to access the view they've navigated to. Silverlight 4 introduced the concept of content loaders into the navigation framework. A custom content loader enables you to add your own logic to the process of loading content into a Frame control.

The navigation framework comes with a default content loader named PageResourceContentLoader, but it only allows you to load views existing in the current XAP file. However, perhaps you'd rather dynamically download XAPs from the server on demand and load views from them instead. This would enable you to split your application up into smaller chunks and download these chunks only when the user needs them, reducing both bandwidth and the load time of your application. This is particularly useful when your application is large and users only tend to use a small portion of it.

When a navigation event takes place, the navigation framework follows this process:

  • The Frame control's UriMapper performs its URI mapping (as normal).
  • The NavigationService passes the content loader the both current view's URI and the mapped URI, and the content loader asynchronously handles the loading of the content.
  • Once the content loader has loaded the content for display, it notifies the NavigationService that it is ready. The NavigationService then calls the content loader's EndLoad method, which returns the content to be displayed in the Frame control, usually a view inheriting the Page class, as part of its result.

Put simply, the content loader is passed in a URI, and it returns the content to be displayed.

To create your own custom content loader, you need to create a class that implements the INavigationContentLoader interface (implementing its BeginLoad, CanLoad, EndLoad, and CancelLoad methods) with the required logic and point the Frame control's ContentLoader property to it.

Going deep into implementing content loaders is beyond the scope of this book, but David Poll has some excellent articles covering the use of content loaders to dynamically download XAP files and loading views from them, as well as performing authentication and authorization when a view has been navigated to. You can find these articles at his blog here: http://www.davidpoll.com/tag/contentloader/. Jeff Prosise also has an excellent article on implementing a content loader that dynamically downloads content, using a different method than David's, at http://www.wintellect.com/CS/blogs/jprosise/archive/2010/06/27/dynamic-page-loading-in-silverlight-navigation-apps.aspx.

images Note We'll look at making use of a custom content loader in Chapter 17 when we discuss breaking down your application into modules and dynamically downloading them when they are required.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset