This is a mirror of official site: http://jasper-net.blogspot.com/

The INavigationContentLoader interface in Silverlight 4

| Tuesday, December 21, 2010
Since Silverlight 3, we have had the Navigation Application template available. Using this template, we did not need any tricks anymore to do navigation between “pages” (which did not exist in Silverlight 2, we only had the UserControl). In SL3, a Navigation Application was however rather locked down: developers had little to none options available to hook into the navigation process itself.

With Silverlight 4, the INavigationContentLoader interface was introduced. This interface provides a means for us to override how the navigation framework works: we can provide our own logic for the page-loading process. Based on the target URI that is passed in, we can load the page, but we can do other actions as well first. Let’s first look at this interface:

public interface INavigationContentLoader
{
   IAsyncResult BeginLoad(Uri targetUri, Uri currentUri,
       AsyncCallback userCallback, object asyncState);
   
   void CancelLoad(IAsyncResult asyncResult);
 
   bool CanLoad(Uri targetUri, Uri currentUri);
   
   LoadResult EndLoad(IAsyncResult asyncResult);
}


Because we can provide whatever functionality we want, this simple interface does much more, we can practically do whatever we want when a user clicks on a link. Think of actions such as the following:
  • The target URI could contain the name of the XAP file in which the XAML view can be found (ex. MyXap.xap/View1.xaml). Our custom logic could check if this XAP file is already available/downloaded on the client. If not, it can be downloaded on-demand.
  • Check that the user has permissions to see the requested URL.
  • Load the ViewModel corresponding to the View that is navigated to.
  • Pass parameters to the constructor of a page

The flow to load a page (and the corresponding calls can be seen in the interface):
  • A check is done using the CanLoad method if the requested page can be found. This can be a location to handle in our own way how errors resulting from navigating to a non-existing page are shown to the user.
  • The page loading process is using a cancellable asynchronous way to load pages, starting with the BeginLoad. In the BeginLoad, we, quite logically, start the loading process.
  • Once BeginLoad started the loading process, the navigation framework waits for the loading to be complete. Once complete, the Endload is called. In normal circumstances, the LoadResult is a fully initialized Page that can now be displayed to the user.

Read more: Snowball - The Blog

Posted via email from .NET Info

1 comments:

Anonymous said...

Your сurгеnt аrticlе
ρrovidеѕ establіshеd bеnеficial tο mysеlf.
It’s ехtremely infоrmative аnd you
rеally аre natuгally reаlly educated οf this type.

Υоu have pοppeԁ my faсe to different
thoughts about thiѕ particulaг subject together with intriquing, notable and ѕounԁ
content material.
my site :: viagra