Handling BackKeyPress in WP7 app?

Apr 4, 2011 at 4:36 PM

My WP7 app has a popup control that is created by one of my viewmodels. When the popup is displayed, hitting the back button navigates to the previous page instead of closing the popup, so I need to handle the BackKeyPress event and dismiss the popup.

What's a good way to implement this with Caliburn.Micro? The conventional approaches will require me to do some mangling of my code. For example, the parent page's viewmodel will need a reference to the parent page view in order to add the popup to it as a child. Is there a better solution?

 

Coordinator
Apr 4, 2011 at 4:44 PM

Have a look at the new WindowManager for WP7 in the latest bits. It has a ShowDialog method that takes a view model, locates the view and shows it as a modal dialog overlay. It integrates with the back button to close the dialog.

Apr 5, 2011 at 3:06 AM

This is great. I think it'll be a big help once I get it set up.

I'm trying to reimplement the simple dialog example from the sample app in my own demo project, and I must be missing something really stupid. The viewmodel for the dialog is being constructed, but the view isn't showing up. I posted a copy of the project here. Can you see where I'm screwing it up?

I'm really needy lately. :) I did tool around with this for a couple of hours first, though. Just can't nail it down.

Coordinator
Apr 5, 2011 at 2:28 PM

What is showing up?

Apr 5, 2011 at 2:30 PM

Nothing shows up at all. I'm expecting the usercontrol to appear; it seems like the viewmodel isn't finding the view, or is failing to instantiate it somehow.

Coordinator
Apr 5, 2011 at 2:38 PM

Your problem is that it cannot locate the view. Your view is in a UserControls namespace and your view model is not. The default view locator maps by removing the word "Model" from the name. So if there are other differences, it cannot locate the view. You either need to move the view out of that namespace or move both things somewhere else. You could also create a custom mapping by altering the ViewLocator.

Apr 5, 2011 at 3:10 PM

You nailed it. Ugh, so close, and yet so far... Really appreciate the help, Rob.