wp7 map.setview

Topics: Getting Started
Jul 21, 2011 at 12:30 PM


I'm new to Caliburn Micro and so far I love the simplicity of it when I'm doing simple things. I thought I'd stretch my legs a little and put a map control on a page and see what I can do. I can pretty much do everything I want, though I can't figure out an elegant way of calling the .SetView() of the map control from the viewmodel.

Can someone shed some light on how I would do this?



Jul 22, 2011 at 2:59 AM
Edited Jul 22, 2011 at 3:00 AM

I haven't used the map control, so I'm going to make an educated guess. In scenarios where you have very complex controls, such as maps, docking window managers, etc...it's often easier to change from using a ViewModel to using a Supervising Controller pattern. In this case, your ViewModel would become a Presenter (SupervisingController flavor) and would call the View through an interface. You can obtain a reference to the view by inheriting from ViewAware or Screen (or a descendant). Override OnViewAttach and you can cast the paramter as your interface and store the reference so that you can ultimately call SetView. Another approach would be to use the EventAggregator. You could have the view subscribe to an event and the view model publish the event. A 3rd way would be to wire an Action to the Loaded event of the view, then use a custom IResult to talk to the View and call the SetView method. That's probably overly complicated though. Either the first or second way is preferable. The SupervisingController way will require a bit more work, but will be more explicit. The EA strategy will be a bit less work, but might be more confusing from a maintenance perspective. Choose what makes the most sense for your scenario.

Jul 22, 2011 at 12:45 PM

Thanks for the help, I implemented OnViewAttach and it works a treat.

Funnily enough I also tried subscribing to an event in the view but the Handle(...) would never fire for me. I couldn't for the life of me figure it out but the first option worked first go so I'll run with that.

Jul 22, 2011 at 11:09 PM

In order to have your view handle events you need to call EventAggregator.Subscribe passing the handler instance (which is the view itself):
(given that your view implements IHandle<>)