Joseph Le Brech
Joseph Le Brech

Reputation: 6653

Going through a Pivot control really fast crashes

I have a pivot control and a button which does selectedIndex++ and when the selectedIndex has gone passed the last entry it will open up a messagebox asking the user if they want to quiz.

But during testing if you spam the button it will create a 0x8000ffff error when you open the MessageBox.

How do I stop this from happening? is it something to do with the ui thread being too busy or continuing to move the pivot? is the button event still running after I try to navigate out of the page?

this is what the code that does the selectedIndex++

void gotoNextQuestion()
{
    if (quizPivot.SelectedIndex < App.settings.currentTest.Questions.Count() - 1)
    {
        //xScroll -= scrollAmount;
        //moveBackground(xScroll);

        if (!stoppedPaging)
        {
            quizPivot.SelectedIndex++;
        }

        //App.PlaySoundKey("next");
    }
    else
    {
        if (App.settings.testMode == App.TestModes.TrainingRecap)
        {
            MessageBoxResult result;

            if (countAnsweredQuestions() == App.settings.currentTest.Questions.Count())
            {
                stoppedPaging = true;
                result = MessageBox.Show("You have reviewed every training question, would you like to go back to the main menu?", "Training Over", MessageBoxButton.OKCancel);
                stoppedPaging = false;
                if (result == MessageBoxResult.OK)
                {
                    positionableSpriteRadioButton.IsAnswered -= new Action<bool>(Answers_IsAnsweredCompleted);
                    spriteRadioButton.IsAnswered -= new Action<bool>(Answers_IsAnsweredCompleted);
                    App.settings.currentTest = null;
                    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                    return;
                }
            }
        }
        else
        {
            MessageBoxResult result;

            if (countAnsweredQuestions() == App.settings.currentTest.Questions.Count())
            {
                stoppedPaging = true;
                result = MessageBox.Show("You have answered all of the questions, are you sure you want to finish?", "Are you sure you want to finish?", MessageBoxButton.OKCancel);
                stoppedPaging = false;
            }
            else
            {
                checkFinishConditions();
            }
        }

        quizPivot.SelectedIndex = 0;
        //App.PlaySoundKey("begin");
    }

    App.settings.currentTest.currentQuestion = quizPivot.SelectedIndex;
}

Upvotes: 2

Views: 163

Answers (1)

Claus J&#248;rgensen
Claus J&#248;rgensen

Reputation: 26347

Well, one thing is for sure

positionableSpriteRadioButton.IsAnswered -= new Action<bool>(Answers_IsAnsweredCompleted); 

That isn't going to work. You're creating a new Action every time. So nothing will have the same reference id, and thus nothing will be removed.

Instead you should remove the Action<bool> and simply subscribe/unsubscribe with

positionableSpriteRadioButton.IsAnswered -= Answers_IsAnsweredCompleted;

And when you subscribe

positionableSpriteRadioButton.IsAnswered += Answers_IsAnsweredCompleted;

That way you can actually remove it again.

But I would recommend you not to use a pivot for this type of "wizard". It's abuse of the control, and going to give a really poor user experience.

Also, just because you navigate to another page, it doesn't mean the code stops running. All code in the same expression is executed, unless you add a return statement after the call to NavigationService.Navigate.

Also, always make sure that Navigation is on the UI thread by wrapping all calls to NavigationService.Navigate in a call to Dispatcher.BeginInvoke.

Upvotes: 2

Related Questions