Pergunta

I'm developing a simple game, and if the user hits the Back Button, I don't want it to go back to the previous page, nor terminate the app. I want to show a MessagePrompt (from Coding4Fun Controls Library) to ask the user if he really wants to go back to main menu (and lose all progress in the game).

Problem is, when I implement the code below, the MessagePrompt does appear, but quickly closes. Any hint what's happening. If the exactly same code (except for the e.Cancel and base.OnKeyPress stuff) is applied in a regular button, the MessagePrompt is properly shown. I'm obviously screwing up something with this BackButton.

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;            
        MessagePrompt abortConfirm = new MessagePrompt();
        abortConfirm.Title = "Are you sure you want to go to Main Menu?";
        abortConfirm.Message = "All progress in the current game will be lost";
        Button confirmAbort = new Button();
        confirmAbort.Content = "Yes";
        confirmAbort.Click += (object sender3, RoutedEventArgs e3) =>
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        };
        Button cancelAbort = new Button();
        cancelAbort.Content = "No";
        cancelAbort.Click += (object sender2, RoutedEventArgs e2) =>
        {
            abortConfirm.Hide();
        };
        abortConfirm.ActionPopUpButtons.Clear();
        abortConfirm.ActionPopUpButtons.Add(confirmAbort);
        abortConfirm.ActionPopUpButtons.Add(cancelAbort);
        abortConfirm.Show();
        base.OnBackKeyPress(e);
    }

Any hints of what's happening?

Iff I try the good old MessageBox (code below), it works like a charm. Problem is, the game is a memory game, so if the user hit the Back Button in the right time, he will be able to partially see the screen below (as MessageBox.Show() pauses everything on background, including my DispatcherTimer. And I am already using MessagePrompt from Coding4Fun for other features, so consistency is also appreciate.

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        if (MessageBox.Show("Are you sure you want to exit?", "Confirm Exit?",
                        MessageBoxButton.OKCancel) != MessageBoxResult.OK)
        {
            e.Cancel = true;
        }
        else NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
    }
Foi útil?

Solução

I'm no expert on coding4fun controls but after testing it seems a fault on their part and not yours.

You have some solution here:

  1. Use another control from another library or built by you.
  2. Change the way your app show the user the message.
  3. Use a workaround for example this seems to work:

    protected override async void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        base.OnBackKeyPress(e);
        await Task.Yield();
        MessagePrompt abortConfirm = new MessagePrompt();
        abortConfirm.Title = "Are you sure you want to go to Main Menu?";
        abortConfirm.Message = "All progress in the current game will be lost";
        Button confirmAbort = new Button();
        confirmAbort.Content = "Yes";
        confirmAbort.Click += (object sender3, RoutedEventArgs e3) =>
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        };
        Button cancelAbort = new Button();
        cancelAbort.Content = "No";
        cancelAbort.Click += (object sender2, RoutedEventArgs e2) =>
        {
            //abortConfirm.Hide();
        };
        abortConfirm.ActionPopUpButtons.Clear();
        abortConfirm.ActionPopUpButtons.Add(confirmAbort);
        abortConfirm.ActionPopUpButtons.Add(cancelAbort);
        abortConfirm.Show();
    }
    

but note that I'm not sure that just yeilding the task will always give you the wanted result. Or you might use a await await Task.Delay(1); but again it's really a bad practice to use a delay for this kind of thing.

Outras dicas

The control does do a hook on back button since the back button can close the prompt.

Create a repro on the c4f toolkit and I'll look into it closer.

http://coding4fun.codeplex.com/WorkItem/Create

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top