Bashir Magomedov
Bashir Magomedov

Reputation: 2881

How to get main page from any part of Windows Phone 7 application?

Is there any way of getting main page object from any of its child controls? As a possible solution I see here bubbling through parents and stopping as soon as a parent is of PhoneApplicationPage type. It is fine for me, but what if I need to do that from other pages? I.e. How to get main page of an application from everywhere within the application?

Upvotes: 6

Views: 3446

Answers (2)

Esa
Esa

Reputation: 1121

I wanted to encapsulate the behavior of a pivot item inside a view class. Apparently, SelectionChanged rotates through every pivot item during startup, so I had to do the following:

var frame = App.Current.RootVisual as PhoneApplicationFrame;

if (null == frame) //during app load
{
   container = App.Current.RootVisual as MainPage;
}
else //during pivot event;
{
   container = frame.Content as MainPage;
}

Container is defined as private MainPage container = null; Weird.

Upvotes: 0

JP Alioto
JP Alioto

Reputation: 45117

In your App.xaml, there is a public property called RootFrame it is of type PhoneApplicationFrame class. It is the container for PhoneApplicationPages as discussed in this article.

You can always get to the RootFrame since it's at application scope. So, for example ...

var frame = Application.Current.RootVisual as PhoneApplicationFrame;
var startPage = frame.Content as PhoneApplicationPage;

The page that your app starts on is in WMAppManifest.xml ...

<Tasks>
  <DefaultTask  Name ="_default" NavigationPage="MainPage.xaml"/>
</Tasks>

Upvotes: 9

Related Questions