Recently David Poll posted a very cool technique for navigating to dynamically loaded pages and on demand downloading of pages in a Silverlight 3 application . It was very cool but the explicit wire up of each page can be a pain in large applications. So having just posted about MEF on Silverlight, I thought this was a great fit. The Managed Extensibility Framework (MEF) is all about discovering and wiring up components and Silverlight 3 Navigation has some very interesting components. But before I could get it done, our Test Manager, Dinesh Chandnani beat me to it. He wrote this very cool example app that I extended just a bit.
The model is very simple. You just create a page (via the Silverlight Page item template) and put metadata on them saying how to navigate to it. The page can be in the same XAP or different XAP. No need to wire them up.
For example, for a page in the same XAP, you just create a page and add some metadata to it
For a page in a different assembly, possibly delay loaded or conditionally loaded:
Pretty much the same, but we use the packURI syntax and David Poll’s Dynamic page class\pattern (see his blog post above for more information).
If you want to load pages from an external assembly, you do need to explicitly load that assembly. But this is pretty easy to do and it can be done at any point in the application. When the user logs in as in an admin role, when advanced functionality is required, after initial startup of the application, etc. The page simply appears in the nav bar when it is downloaded and running. For example, I extended Dinesh’s code by adding an assembly that was loaded when a button is clicked. The code is simple:
When the button is clicked, we download kick off an async download of the XAP (line 3-4). Then when it is downloaded it we add it to the catalog the nav bar is tied to in line 12.
Now, how does all this work? There are afew parts really.
First we define the PageMetadataAttribute in the main assembly. All the pages will have to reference this, so you might want to factor it out into its own assembly..
A few notes:
Line 1: We set this up as a MEF Metadata attribute.. this enables the MEF engine to get composition information from this attribute
Line 2: We set this up such that it can be applied only once per type. MEF uses this as a hit on how to compose it.
Line 3: we derive from ExportAttribute, this enables us to completely hide the MEFness out of the picture… folks writting pages don’t need to know MEF is under the covers at all.
Line 4: Say that this attribute will be used on FrameworkElements, a hit to MEF on how to type things.
Lines 6-7: just set up the attribute interface we want.
In MainPage.cs, we have some code that populates the nav bar as new pages are discovered.
We have a Pages collection that we have marked with AllowRecomposition, which means its value’s can change over the life of the application (such as when a new package is added as I showed above).
Then we simply update the links whenever new items are available in the Page collection.
Notice in line 12-16 we are dynamically creating the Xaml elements to add into the tree.
The final bit of code we added was in App.cs to set MEF..
Here we setup the catalog to enable MEF to look for pages in the current assembly and in an assembly we download dynamically after the app is up and running. Line 8 sets up the call to the handler below when the page has been downloaded…
For such as simple example on my dev machine the page downloads immediately so you can’t really tell.. so I added a brief delay so you can see it pop it easier. So the Thread.Sleep() in line 3 is there to demo better… I highly recommend removing it in production code.
That is all there is to it! A very simple solution.
For more information see: