Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current page in my WP7 app

Tags:

Is there a way to get the active instance of the current page in my Windows Phone 7 app? I have been reading documentation and trying to figure it out to no avail.

In my example I am running a global loop which calls a public method of the active page.

For example, the application loads to Page1.xaml, if my loop fires with this page active I need to fire a loop found in the Page1 code-behind. If Page2 is active I will need to be firing a different method in the Page2 code-behind file.

NOTE: My methods in the individual pages cannot be static because I require access to instance variables. So not only do I need the active page (which could be achieved through a variable of some sort), but I need the instance of the current page.

like image 599
andersra Avatar asked Mar 02 '11 03:03

andersra


1 Answers

You can get the current page from the root frame. The default App class that is generated in the project has a RootFrame property that is already of type PhoneApplicationFrame. The value of the Content property is the current page and can be cast to PhoneApplicationPage.

var currentPage = ((App)Application.Current).RootFrame.Content as PhoneApplicationPage;

The RootVisual property on the Application class references the same frame, but is a UIElement, so you could use the following instead:

var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;
like image 200
Derek Lakin Avatar answered Nov 29 '22 07:11

Derek Lakin