Tom Xue
Tom Xue

Reputation: 3365

How to get the page leaving event in UWP?

I mage my UWP page to be Singleton mode. So that I cannot use function Navigate() to navigate to this singleton page. The Navigate() function's replacement method is as below.

 this.Frame.Content = new BlankPage2();
 Window.Current.Activate();

But with this way, when we leaving a page, the OnNavigatingFrom() of the page will not be called. But I need an event to tell me whether a page is leaving. How?

Upvotes: 0

Views: 192

Answers (1)

Martin Zikmund
Martin Zikmund

Reputation: 39112

This seems like an unusual design decision, but in any case, you are able to get notified on when the Frame.Content property changes:

var token = rootFrame.RegisterPropertyChangedCallback(
               Frame.ContentProperty, 
               ContentChanged);

The ContentChanged method can look like this:

private void ContentChanged(DependencyObject sender, DependencyProperty dp)
{
    //do something when Content changes
}

The token variable above can be used to unregister the property change notification later:

rootFrame.UnregisterPropertyChangedCallback(Frame.ContentProperty, token);

Upvotes: 2

Related Questions