locked
HOw to navigate or load pages ?

    Question

  • CAn someone explain the difference between the below snippets.

    Navigating to a page, loading the page, how are these 2 events different from each other ?

     auto rootFrame = ref new Frame();
       	if (!rootFrame->Navigate(TypeName(MainPage::typeid), datamanger->DataSource))
    	{
    	    throw ref new FailureException("Failed to create initial page");
    	}
    
    	Window::Current->Content = rootFrame;
    	Window::Current->Activate();

    auto pnewPage = ref new NewPage();
    Window::current::Content = pnewPage;
    Window::Current->Activate();
    

    Wednesday, June 25, 2014 6:48 PM

Answers

  • A frame can host and navigate between multiple pages. The first uses a frame and so you can navigate it from the original MainPage to other pages. See Navigating between pages

    The second loads the page directly and so there is no navigation. It will stay on the original NewPage, barring you implementing your own navigation system and directly meddling with the Content.

    The first is the most common and is what the general templates use. The second is common for games and is what the DirectX templates use.

    • Marked as answer by Minu7 Wednesday, June 25, 2014 7:16 PM
    Wednesday, June 25, 2014 7:05 PM
    Owner

All replies

  • A frame can host and navigate between multiple pages. The first uses a frame and so you can navigate it from the original MainPage to other pages. See Navigating between pages

    The second loads the page directly and so there is no navigation. It will stay on the original NewPage, barring you implementing your own navigation system and directly meddling with the Content.

    The first is the most common and is what the general templates use. The second is common for games and is what the DirectX templates use.

    • Marked as answer by Minu7 Wednesday, June 25, 2014 7:16 PM
    Wednesday, June 25, 2014 7:05 PM
    Owner
  • That's very clear. Thanks Rob.

    I think now i understand the reason on what might cause the issue i posted here.

    On app launch, I load a page and that page has got its own Windowsizechanged event handler.

    Now, when i load another page on a button click, this first page is not getting unloaded. As a result, when i resize the window of second page, i receive a callback to the first page's windowssizechanged handler.

    So, i have had all layoutawarepages and had no problems whenever my windows were resized. Now, as i'm providing a specific resizehandler in the new pages, i think that is causing this issue. Please let me know if you see any other issues here (I mean you know like when i have layoutaware pages combined with unaware ones).

    http://social.msdn.microsoft.com/Forums/windowsapps/en-US/68cff131-ecf3-4424-bf75-1287f8f2ca11/page-unloaded-is-not-get-fired?forum=winappswithnativecode 

    Wednesday, June 25, 2014 7:16 PM
  • +1 what Rob said. I just want to add that LayoutAwarePage is deprecated in 8.1. It will still work when you retarget a project to 8.1 from 8.0, but it's not used in the new project templates and some of the APIs it uses are also deprecated and might disappear in future versions of Windows. The ideal solution is to use the new 8.1 templates such as Basic Page which have layout-awareness based on Page_sizeChanged.
    Friday, June 27, 2014 5:33 PM
  • You mean to say, handle Windows_Sizechanged event ?
    Saturday, June 28, 2014 3:50 AM
  • As Rob said earlier, I understand the difference between navigating to page through frame and a page_load.

    I have a case, where in, I use page_load event and do some work in the handler.

    Now, as i said earlier, i've had some memory leaks while subscribing to WindowsSizeChanged event handler and when i navigate (or load ) the next page.

    auto pnewPage = ref new NewPage();
    Window::current::Content = pnewPage;
    Window::Current->Activate();

    Like this, I load the new page and in the new page's constructor, i subscribe to both Windowssizechange and UnLoaded event handlers.

    I've a button on that page, click on that will take users to the next page (i load it similarly)

    Now, when i try to use frames and navigate to a page and then load it, i don't see the unload call firing at all. I'll post a sample soon.

    But before that, just wanted to check in this. When will this UnLoaded event be fired ? it should be whenever the visualelement is removed from the tree correct ? I've had my code to unsubscribe from sizechange event in the unloaded handler actually.

    thanks !

    Saturday, June 28, 2014 7:29 PM
  • Are you using a lambda expression, or a named function for your event handler delegate? Lambdas can cause memory leaks. From MSDN topic on Delegates

    In general, for an event handler, it's better to use a named function instead of a lambda unless you take great care to avoid circular references. A named function captures the "this" pointer by weak reference, but a lambda captures it by strong reference and creates a circular reference. For more information, see Weak references and breaking cycles (C++/CX).

    Monday, June 30, 2014 6:17 PM