WP7 - Modify the ApplicationBar in ViewModel

Topics: Getting Started
Jan 17, 2012 at 10:41 AM

Hello again,

I'm currently trying to figure out a "clean" way of changing the Buttons of the ApplicationBar depending on which pane of a Panorama control is selected. Not just their text and icon, but the number of buttons as well. Also, the AppBar changes between being "Visible" and "Minimized", a lot like the People Hub, which I'm modelling my app after. However, the ApplicationBar can only be accessed by the View, so I need to get a reference to it into the ViewModel somehow.

Before I started to use CM, I would pass a reference to the ApplicationBar from the View via the ViewModel constructor. From what I understand, though, CM takes care of instantiating the ViewModel and ViewModel constructors seem to be expected to take a single INavigationService argument, meaning that path is no longer valid.

Is there a way in Silverlight / C# / CM to accomplish what I'm trying to do?



Jan 17, 2012 at 3:06 PM

There is nothing out-of-the-box to assist in dynamically changing the AppBar. I would like to create a service for this in the future. If you would like to see this in the next version, I would appreciate it if you created a ticket as a feature request and wrote up a nice description and scenario. Thanks! Sorry for the inconvenience. We are slowly but surely trying to make WP7 dev a better experience. There are still a few pieces left to go though.

Jan 17, 2012 at 3:17 PM

Thanks for the answer, I'll create a ticket then :)

Meanwhile, can you give me any hints on a work-around? I.e. any way to get that reference into the PanoramaViewModel while still using the ViewModelLocator? Or, actually, a way to tell CM to not use the ViewModelLocator for this specific view, because I did have it all working, but started using Caliburn because it makes a lot of other things much easier :)

Either way, your work is much appreciated!

Jan 17, 2012 at 5:10 PM
Edited Jan 17, 2012 at 5:11 PM

If your PanoramaViewModel inherits from Screen (or is a Conductor or ViewAware subclass or implements IViewAware) you can override/implement OnViewAttached/AttachView or OnViewLoaded. This will give you access to the view that your view model is bound to. You can then do any sort of manipulation of the view that you need. I recommend that you put as much of that code as possible in the View, so you can invoke it very simply from the VM. Would that help you in the mean time?

Jan 17, 2012 at 5:29 PM

Yes, that's exactly what I needed, thank you very much =)