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

♀尐吖头ヾ 提交于 2019-12-10 10:37:48

问题


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.


回答1:


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)
        {
        }
    }



回答2:


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.



来源:https://stackoverflow.com/questions/13653694/page-onnavigatedto-doesnt-get-called-in-windows-store-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!