locked
"pageRoot_Unloaded" runs twice RRS feed

  • Question

  • Hello

    I have a Problem, that a Page I Close (or leave) and wants to fire the "pageRoot_Unloaded"-Method, i figure out that this Mathode is called twice.

    Can anybody say why this happen ?

    Here is my code

    private async void pageRoot_Unloaded_1(object sender, RoutedEventArgs e)
            {
                int i = 0;
                bool x = false;
                bool? MDresult = null;
    
                // mindestens eine Fragenkategorie muss ausgewählt werden
                for (i = 1; i < 12; i++)
                {
                    if ((App.Current as App).vKAT[i] == true)
                    {
                        x = true;
                    }
                }
                if (x == false)
                {
                    MessageDialog MD = new MessageDialog("Mindestens eine Kategorie muss ausgewählt werden!", "ACHTUNG!");
                    MD.Commands.Add(new UICommand("OK", new UICommandInvokedHandler((cmd) => MDresult = true)));
                    await MD.ShowAsync();
                    return;
                }
                (App.Current as App).WriteSettingsFile();
            }
    thanx a lot !

    Darko Grgic (MCSE,MCTS)


    Monday, March 11, 2013 6:23 PM

Answers

  • Hi

    thanx guys

    i hav profed the call stack and figured out, that I call the Methode only once, but the Event was "lostFocus".

    I Change the Event in "Unloaded" ant everything work fine !

    Thanx a lot !

    Darko


    Darko Grgic (MCSE,MCTS)

    • Marked as answer by Darko Grgic Tuesday, March 12, 2013 4:46 PM
    Tuesday, March 12, 2013 4:46 PM

All replies

  • How are you registering for the Unloaded event? Might you be registering for it twice?

    I'd also avoid showing your warning in a MessageDialog. You will have a smoother experience if you notify the user of the error in-line so they can fix it without going back and forth and losing their context. See Choosing the right UI surfaces: Errors

    --Rob

    Monday, March 11, 2013 8:39 PM
    Moderator
  • Check the Callstack from where the multiple calls are being made and remove the un-necessary unload event. You can set a breakpoint at your function's entry point and check the callstack when the breakpoint is hit.
    Monday, March 11, 2013 8:47 PM
    Moderator
  • Hi

    thanx guys

    i hav profed the call stack and figured out, that I call the Methode only once, but the Event was "lostFocus".

    I Change the Event in "Unloaded" ant everything work fine !

    Thanx a lot !

    Darko


    Darko Grgic (MCSE,MCTS)

    • Marked as answer by Darko Grgic Tuesday, March 12, 2013 4:46 PM
    Tuesday, March 12, 2013 4:46 PM