Query String in ViewModel

Topics: Prism v4 - Silverlight 4
Dec 7, 2010 at 9:10 PM
Edited Dec 7, 2010 at 9:10 PM

I am a bit unsure how to bring a querystring value from the view into the view model. 

I am so far using a very simple method of specifying the viewmodel as the datacontext of the view. 

In my situation I have a ModuleFrameView that has a menu control on it. The menu is populated from data returned from a RIA data service. There really is no easy way to build the UI control in the viewmodel (or is there) so I am building it in the code behind.

The ModuleFrameView is reused based and the module name is passed to it. The moduleId is passed to it in the query string. All of the code to get data from the service (or just build some sample data if in design mode) is in the view. I can see a way to get the querystring data in the viewmodel. 

So what I did was expose a method in the view model called LoadMenus that accepts the model id and also the collection of entities. I also exposed an event that is raised once the RIA service returns the data. In the code behind of the View I cast the Datacontext to the ViewModel type, assign a handler to the MenusLoaded event and then call the menus loaded passing in the value from the query string. (This is done in the OnNavigatedTo event handler, I'm using basic SL4 navigation not prism regions).

This all seems to work but is also seems wrong and brittle. Is there a better way to get the querystring data in the view model? 

It seems to me that there would be a lot less event gymnastics if I just did everything in the code behind. If there is a way to build the menu control in the viewmodel I just don't see it. Ingragistics has not been able to get me a sample of binding the menu to a property in the viewmodel. 



Dec 9, 2010 at 7:09 PM

Hi Bob,

As explained in this chapter from the Prism documentation, there is no guidance on how to use Silverlight Navigation with MVVM in Prism out of the box.

From the documentation, “By default, the Silverlight navigation framework does not directly support the use of the MVVM pattern or the user of a dependency injection container or MEF. However, you can implement a custom content loader—the component used by the frame to load the content associated with a specific URI—which can instantiate and initialize the view and its associated view model as appropriate and display it in the Frame. You can also implement a custom content loader to more fully integrate Silverlight's navigation framework with the Prism region navigation mechanisms.

You can, however, read more about Silverlight Navigation with MVVM pattern here.

Additionally, take into account that Prism Region Navigation is not designed to replace your Silverlight Navigation approach, so you might benefit from it while keeping your current scenario.

I hope you find this information useful.


Miguel Bronzovic


Dec 9, 2010 at 7:52 PM

You can also look at Karl's article on Prism Navigation with Silverlight here: http://blogs.msdn.com/b/kashiffl/archive/2010/10/05/integrating-prism-v4-region-navigation-with-silverlight-frame-navigation.aspx

There is a little bit of work involved in doing this, but we ensured that we included all the necessary hooks to allow this to work and did a lot of proof of concept work as well. Unfortunately due to time constraints, we could not move the ideas from the proof of concept into a real quickstart and get it up to our ship quality bar.

I hope this helps,

Dec 9, 2010 at 10:43 PM

I'm not sure how this info addresses how to pass data from a view to a viewmodel? The navigation is working fine. Would I need to use the EventAggragation in prism or is there a better way to get querystring data from the view to the view model?

From what I've seen any method of MVVM in Silverlight is always View first... in other words... the view is what silverlight loads as opposed to say MVC where the controler is instantiated first and it specifies what view to render.

I am NOT using Prism Regions btw.