0

I am using the KeepAlive property on a page so that I am able to navigate back to it in the state that the user left it in.

The user is able to search for a Company, look at further details on the Company in an additional page, and then navigate back to the search page in the search state they left it in.

I also take advantage of the Loaded event on a page to set it up in a way that a users favourite Companies are displayed first.

After searching, when the user navigates back from the details page inevitably the Loaded event is fired and their search is reset, back to the state depicted in the Loaded method.

My question is this - is there a way I can fire the Loaded event when the page is loaded for the first time, however when it is KeptAlive and navigated back to, the Loaded event is not fired?

I would also prefer a way to unattach this behaviour because if a user truly does navigate away from the search page (i.e NOT to the details page) I would want the page to fire the Loaded event the next time said user navigates to it.

EDIT:

var estimatingContractsPage = new EstimatingContractsPage(selectedItem); NavigationService?.Navigate(estimatingContractsPage);

CBreeze
  • 2,925
  • 4
  • 38
  • 93

1 Answers1

3

The Loaded event will be fired each time the Page is actually loaded into your UI. But if you only create a single instance of the Page class, you could use a boolean flag to make sure that the code in the event handler only runs once:

public partial class Page1 : Page
{
    public Page1()
    {
        InitializeComponent();
        this.Loaded += Page1_Loaded;
    }

    private bool _isLoaded;
    private void Page1_Loaded(object sender, RoutedEventArgs e)
    {
        if(!_isLoaded)
        {
            //Your code

            _isLoaded = true;
        }
    }
}

Another option would be to simply unhook the event handler once it has been invoked:

public Page1()
{
    InitializeComponent();
    this.Loaded += Page1_Loaded;
}

private void Page1_Loaded(object sender, RoutedEventArgs e)
{
    //Your code
    this.Loaded -= Page1_Loaded;
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • A simple answer in reality thanks. I'm still not quite sure how I'll handle actually navigating away to somewhere other than the details page - in that case I would want the page to be 'Loaded' when the user comes back. – CBreeze Jun 09 '17 at 10:23
  • The solution to this is to know when you navigate to another page. How are you navigating between pages? – mm8 Jun 09 '17 at 10:28
  • Check the edit for how I am navigation. I could check the name of the page when navigation and hook/unhook the loaded event handler I assume. I could hook into the NavigationService.Navigating event but I'm not sure how I could check the destination of the event.. – CBreeze Jun 09 '17 at 10:52
  • Yes, for example. It is only a matter of defining the condition, i.e. instead of simply checking the value of the _isLoaded flag, you could check any other condition. The Point is that the event will be raised but your code in the event handler will only be executed under certain circumstances. – mm8 Jun 09 '17 at 10:55