Luke Puplett
Luke Puplett

Reputation: 45135

Page.OnNavigatedTo doesn't get called in Windows Store app

I have a Windows Store mess around app. I added a Basic Page and it added the Common classes, such as LayoutAwarePage.

But Page.OnNavigatedTo doesn't get called when an app starts. The MSDN doc says:

Invoked when the Page is loaded and becomes the current source of a parent Frame

Which happens during launch. I discovered this when LoadState wasn't being called.

Rick Barraza uses LoadState, which is called by OnNavigatedTo, in his demo:

http://channel9.msdn.com/Series/Migrating-apps-from-Windows-Phone-to-Windows-8/Binding-the-Interface-in-Windows-Store-apps#time=2m50s

I know something is broken because now that I've added some navigation buttons, the OnNavigatedFrom is called but falls over because _pageKey is null since it wasn't set by OnNavigatedTo.

I actually am pretty stuck. This is a failing in Microsoft's native Page class, but clearly I'm the only person to have this issue and its 100% discoverable. Odd.

Update 1

I added a new Grid App project and that works. The Common stuff is all there as standard but it does differ from the Common stuff that's written when you add a Basic Page to an empty app.

I will try and repro this from a fresh empty app.

Update 2

Well, I give up. A new Blank App and adding a new Blank Page is fine. I'll just copy and paste my page over and pretend it never happened.

Upvotes: 2

Views: 3909

Answers (2)

Carl Chouinard
Carl Chouinard

Reputation: 186

I had the same problem and it was due to the presence of an empty override of OnNavigateTo in the basic pages I've added.

public sealed partial class MvvmView1
    {
        public MvvmView1()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }

Upvotes: 3

Zack Weiner
Zack Weiner

Reputation: 674

In order for OnNavigatedTo to get called, your Frame must call its Navigate method.

Frame localFrame = this.MyFrame; //this assumes MyFrame is Frame that exists in xaml and has a name 

localFrame.Navigate(new myPage());

If you are using content injection

localFrame.Content  = new myPage();

The OnNavigatedTo event of myPage will not fire, because the page is loaded only, not navigated to.

Upvotes: 1

Related Questions