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
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;
You can get the current XAML page URL using NavigationService.CurrentSource http://msdn.microsoft.com/en-gb/library/system.windows.navigation.navigationservice.currentsource(v=VS.92).aspx
However, to get the instance of the current page itself I think you'll need to maintain this yourself - one way would be to inherit all your pages from a shared abstract class which overrides OnNavigatedTo and then uses this as an opportunity to record itself with a singleton (maybe your App class)