views:

48

answers:

1

Hello,

I made a usercontrol that contains a togglebutton. When the user clicks on it, it shows a fullscreen-sized popup. I want to add the following functionality: when the user presses the hardware back button and the popup is opened, close the popup. The problem is that only the parent page of the usercontrol has backkeypress event. How can I handle this inside the usercontrol which is a reusable control? I try to avoid handling backbutton press in the page's code, so handling this like calling a method of the usercontrol from the page's OnBackKeyPress eventhandler is the last thing I'd like to do (the page is programmatically generated)...

A: 

The control will need to find a reference to the page somehow - either in the constructor, or by walking up the UI tree until it finds a PhoneApplicationPage. It can then subscribe to the event itself, and unsubscribe from the event when it's closed.

Jon Skeet
Thanks! Now I have another problem: I found the parent page, but the eventhandler is never called within my usercontrol :(parentPage = FindParentPage(this);if (parentPage != null){ parentPage.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(parentpage_BackKeyPress);}
Vic
I have to cast parentPage to MainPage when adding the eventhandler...
Vic