SMM
SMM

Reputation: 201

How OnNavigatedTo() invoke only once?

I created a List <> in the method OnNavigatedTo ().

Having called the List <> named Reg, in the method: Reg.Add (....). This means that every time I go on that page I added the elements, and if I change the page and with the Back button of the device I go back, I left again with that method and the elements are dub, triple, etc ..

Is there a way to invoke a method only the first time, and perhaps to recognize whether it is a new navigation, or if on that page we returned only by the pressure of the Back button of the device?

 protected  override void OnNavigatedTo(NavigationEventArgs e)
        {  


                reg.Add(
                    new Regioni
                    {
                        NomeRegione = "Toscana",
                        NomeProvincia = "Firenze"
                    });

                reg.Add(
                    new Regioni
                    {
                        NomeRegione = "Toscana",
                        NomeProvincia = "Prato"
                    });

Upvotes: 0

Views: 137

Answers (2)

fillobotto
fillobotto

Reputation: 3775

You can check whether you reached the page using back button:

protected  override void OnNavigatedTo(NavigationEventArgs e)
{  
    if (e.NavigationMode != NavigationMode.Back)
    {
        // add items to your collection
    }
}

Good luck with development my Italian friend ;)

Upvotes: 2

Emond
Emond

Reputation: 50672

You could add an instance field:

protected bool Initialized = false;

protected  override void OnNavigatedTo(NavigationEventArgs e)
{  
    if(!Initialized)
    {
        Initialized = true;
        reg.Add(
            new Regioni
            {
                NomeRegione = "Toscana",
                NomeProvincia = "Firenze"
            });

        reg.Add(
            new Regioni
            {
                NomeRegione = "Toscana",
                NomeProvincia = "Prato"
            });
    }
}

Upvotes: 1

Related Questions