locked
Page Unloaded is not get fired

    Question

  • Hi,

    I've added a new layoutaware page in my metro app (with VS2013/Win8.1). It uses relaycommand and navigation helper.

    This new page's window resize event handler is called for the other pages too. I mean when this page is loaded, i have subscribed to windows size changed event handler for transitioning to different visual states. Now, what happens is, this page is load and on a button click, I move to another page. When i resize the second page, the first page's windows resized event handler is getting notified (which should not happen right ?). Essentially first page should unload when i  move to next page correct. Please see below how i do it and help me find the root cause of this issue.

    void SamplePage::OnNavigatedTo(NavigationEventArgs^ e)
    {

    //NavigationHelper->OnNavigatedTo(e);

    this->Loaded += ref new Windows::UI::Xaml::RoutedEventHandler(this, &SamplePage::Page_Loaded);
    this->Unloaded +=ref new Windows::UI::Xaml::RoutedEventHandler(this, &SamplePage::Page_UnLoaded);




    }

    void SamplePage::OnNavigatedFrom(NavigationEventArgs^ e)
    {
    //NavigationHelper->OnNavigatedFrom(e);

    // Unsubscribe to window resize events

    //this->Unloaded += ref new Windows::UI::Xaml::RoutedEventHandler(this, &SamplePage::Page_UnLoaded);

    Window::Current->SizeChanged -= sizeChangedToken;
    }

    void SamplePage::Page_Loaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {

    // Subscribe to window resize events
    sizeChangedToken = Window::Current->SizeChanged += ref new WindowSizeChangedEventHandler(this, &SamplePage::WindowSizeChanged);
    // Run layout logic when the page first loads
    InvalidateLayout();
    }


    void SamplePage::WindowSizeChanged(Platform::Object ^sender, Windows::UI::Core::WindowSizeChangedEventArgs ^e)
    {
    InvalidateLayout();
    }

    void SamplePage::InvalidateLayout()
    {

    String^ visualState = DetermineVisualState(Window::Current->Bounds.Width, Window::Current->Bounds.Height);


    VisualStateManager::GoToState(this, visualState, true);


    }


    void SamplePage::Page_UnLoaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {

    Window::Current->SizeChanged -= sizeChangedToken;


    }

    Thanks !!


    • Edited by Minu7 Tuesday, June 24, 2014 3:06 AM corrections
    Tuesday, June 24, 2014 3:06 AM

All replies

  • Please post code snippets in a snippet block. It is very hard to read when they are just pasted in willy-nilly. Including a link to your Onedrive with a minimal sample demonstrating the problem would also be helpful.

    It looks like you may be wiring up the Loaded event multiple times but only unwiring it once. That could leak handlers later in the process.

    --Rob

    Tuesday, June 24, 2014 11:41 PM
    Moderator
  • Thanks, will try to do that in future.

    But, here, I've registered for loaded event only once in my OnNAvigatedTO method, correct ?

    this->Loaded += ref new Windows::UI::Xaml::RoutedEventHandler(this, &SamplePage::Page_Loaded);

    So, can you please tell me when will this page be removed from the visual tree?

    i'm launching next page in a button click like this

    auto pnextPage = ref new nextPage();
    
    Window::Current->Content = pnextPage;
    Window::Current->Activate();


    Wednesday, June 25, 2014 3:02 AM
  • I can't diagnose your code if I can't read it. Please post a minimal sample to your OneDrive if you need help analysing what it is doing.
    Wednesday, June 25, 2014 8:49 PM
    Moderator