MessagePrompt does not work in OnBackKeyPress Event or PhoneApplicationPage_BackKeyPress Event

Nov 30, 2011 at 1:56 AM

I want to use MessagePrompt to replace MessageButton when press BackKey button. But the MessagePrompt doesn't work.

my code:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            MessagePrompt message = new MessagePrompt();
            message.Title = "Title";
            message.Message = "test";
            message.IsCancelVisible = true;
            message.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(message_Completed);
            message.Show();
            base.OnBackKeyPress(e);
        }


void message_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
        {
            //throw new NotImplementedException();
            if (e.PopUpResult == PopUpResult.Ok)
            {
                //do something
            }
            else
            {
                //do something
            }
        }

Can anyone please tell me how to use it?

Thanks! 8-)

Coordinator
Dec 1, 2011 at 7:39 PM

You are destroying your visual tree by going back so while it the prompt is being created, it is actually destroyed by nav'ing back.  Do something like this:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
var t = new Thread(ToastCall);

t.Start();            
            base.OnBackKeyPress(e);
        }


private void ToastCall()
{
Dispatcher.BeginInvoke(() =>
{
MessagePrompt message = new MessagePrompt();
message.Title = "Title";
message.Message = "test";
message.IsCancelVisible = true;
message.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(message_Completed);
message.Show();


});
}

Dec 2, 2011 at 2:33 AM

thanks! It works now. But the page goback before i choose ok or cancel. I want to use it like the MessageButton,the app show the message first before goback. anyway,thanks a lot! 8-)

Coordinator
Dec 7, 2011 at 8:53 PM

well, that is a different behavior than what you asked for above.

here is some sample code: 

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
	var prompt = new MessagePrompt {IsCancelVisible = true, Title = "Test Title", Message = "back override.  ok == allow back to continue; cancel == stay"};
	prompt.Completed += prompt_Completed;
	prompt.Show();

	e.Cancel = true;
	base.OnBackKeyPress(e);
}

void prompt_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
{
	if (e.PopUpResult == PopUpResult.Ok)
	{
		// user hit ok
		NavigationService.GoBack();
	}
	else
	{
		// user hit cancel or back button
	}
}

 

 

Dec 12, 2011 at 2:01 AM

thanks!It looks like normal now. 


But there's a new problem. when I press back key again after the message prompt showed, a new message prompt shows again then go back the previous page. If I click the OK or Cancel button, the navigation goes back the previous page directly and works well.


I dont know what events happen when I press back key when the message prompt is showing.

Coordinator
Dec 12, 2011 at 3:06 AM

yup because back button is hooked into the prompt and will cancel it.  So when the prompt is up and you hit back, it dismisses it.  BUT the back button overridden in the page which then creates a new prompt.

I'm just attempting to get you partly there. 

MessagePrompt _prompt;
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
	if (_prompt == null || !_prompt.IsOpen)
	{
		_prompt = new MessagePrompt
				    {
				        IsCancelVisible = true,
				        Title = "Test Title",
				        Message = "back override.  ok == allow back to continue; cancel == stay"
				    };
		_prompt.Completed += prompt_Completed;
		_prompt.Show();

		e.Cancel = true;
	}
	base.OnBackKeyPress(e);
}

void prompt_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
{
	if (e.PopUpResult == PopUpResult.Ok)
	{
		// user hit ok
		NavigationService.GoBack();
	}
	else
	{
		// user hit cancel or back button
	}
}
Dec 12, 2011 at 3:56 AM

thanks a lot!   Too amazing! It works well now. You r a good teacher! 8-)