bhaku
bhaku

Reputation: 448

how to display a pop up message with ok and cancel button in windows phone 7 application development

I am building an app for windows phone 7. I want to display a messagebox with both OK and Cancel button. Moreover the message box should come in the center of the application. Is it possible to have a design in my messagebox other than the message box shown by defaut?

My code is:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Documents;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Animation;
 using System.Windows.Shapes;
 using Microsoft.Phone.Controls;

 namespace KejriwalPhoneApp
{
public partial class Donate : PhoneApplicationPage
{
    public Donate()
    {
        InitializeComponent();

    }

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    var customMessageBox = new CustomMessageBox()
    {
        LeftButtonContent = "Ok",
        RightButtonContent = "Cancel",
        Content = new TextBlock()
        {   // All TextBlock (or other control) properties are work here
            Text = "This Will Take You Out Of The App And Forward You To Our Contribution Website!",
            TextWrapping = TextWrapping.Wrap
        }
    };

    customMessageBox.Dismissed += (sender, args) =>
    {
        if (args.Result == CustomMessageBoxResult.LeftButton)
        {
            // Action when "ok" pressed
            donation.Source = new Uri("http://mobiledonation.vzons.com/ArvindKejriwal", UriKind.Absolute);

            donation.Loaded += (object sender, RoutedEventArgs e) =>
            {
                donation.Navigate(new Uri("http://mobiledonation.vzons.com/ArvindKejriwal", UriKind.Absolute));
            };
        }
        else if (args.Result == CustomMessageBoxResult.RightButton)
        {
            // Action when "cancal" pressed, optional
        }
        else
        {
            // Action when back key pressed, optional
        }
    };

    customMessageBox.Show();
}

    }
}

}

This is my complete code now for displaying a pop up message. I am getting the following error:

1. The type or namespace name 'CustomMessageBox' couldnot be found
2. The name CustomMessageBoxResult doesnot exist in the current context
3. A local variable named 'sender' cannot be declared in this scope because it would    give a different meaning to 'sender', which is already used in a 'parent or current' scope to denote something else
4. A local variable named 'e' cannot be declared in this scope because it would give a     different meaning to 'e', which is already used in a 'parent or current' scope to   denote something else
5. The name 'CustomMessageBoxResult' does not exist in the current context  

Please help me to fix these

Upvotes: 1

Views: 3870

Answers (3)

Dragon
Dragon

Reputation: 136

Don't use any other library Message box just use windows Default Message with some editing

MessageBoxResult m = MessageBox.Show("Heading", "What do want to say to user so that he/she can press ok or cancel", MessageBoxButton.OKCancel);
if (m == MessageBoxResult.Cancel)
{
   //do what you want when user press cancel
}
else if (m == MessageBoxResult.OK)
{
   //Do what you want when user press ok
}

Upvotes: 4

Oleg Koposov
Oleg Koposov

Reputation: 173

You can use CustomMessageBox from the WP toolkit. So add toolkit, include Microsoft.Phone.Controls into your .cs file. CMB can't be shown while the page is not completely loaded, so your code should look like this:

    public Donate()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        var customMessageBox = new CustomMessageBox()
        {
            LeftButtonContent = "Ok",
            RightButtonContent = "Cancel",
            Content = new TextBlock()
            {   // All TextBlock (or other control) properties are work here
                Text = "This Will Take You Out Of The App And Forward You To Our Contribution Website!",
                TextWrapping = TextWrapping.Wrap
            }
        };

        customMessageBox.Dismissed += (sender, args) =>
        {
            if (args.Result == CustomMessageBoxResult.LeftButton)
            {
                // Action when "ok" pressed
                donation.Source = new Uri("http://mobiledonation.vzons.com/ArvindKejriwal", UriKind.Absolute);

                donation.Loaded += (object sender, RoutedEventArgs e) =>
                {
                    donation.Navigate(new Uri("http://mobiledonation.vzons.com/ArvindKejriwal", UriKind.Absolute));
                };
            }
            else if (args.Result == CustomMessageBoxResult.RightButton)
            {
                // Action when "cancal" pressed, optional
            }
            else
            {
                // Action when back key pressed, optional
            }
        };

        customMessageBox.Show();
    }

CMB doesn't stop a program flow, so what you want to process only after you get result of the CMB, should be added to CMB Dismissed event.

Upvotes: 2

A.K.
A.K.

Reputation: 3331

Other possible way is to use Microsoft.Xna.Framework.GamerService MessageBox(no 3rd party toolkit needed)

add a reference to Microsoft.Xna.Framework.GamerService library and also add a using directive on the top of the page where the message is to be shown up

Just follow the code below

 List<string> messageboxitm = new List<string>();
 messageboxitm.Add("Okay");
 messageboxitm.Add("Cancel");
 string title = "Confirm Buy";
 string displaymessg = "You must buy the app to go further, do you want to buy the app?"; 
 IAsyncResult result1 = Guide.BeginShowMessageBox(title, displaymessg, messageboxitm, 0, MessageBoxIcon.Alert, new AsyncCallback(OnBuyMessageclosed), null);

private void OnBuyMessageclosed(IAsyncResult ar)
        {
            int? buttonIndex = Guide.EndShowMessageBox(ar);
            switch (buttonIndex)
            {
                case 0:
                    Deployment.Current.Dispatcher.BeginInvoke(() => ConfirmBuy("Okay"));
                    break;
                case 1:
                    Deployment.Current.Dispatcher.BeginInvoke(() => ConfirmBuy("Cancel"));
                    break;
                default:
                    break;
            }
        }


  public void ConfirmBuy(string title)
    {
        if (title == "Okay")
        {
            Dispatcher.BeginInvoke(() =>
            {
               //Action
            });
        }
        else
        {
            return;
        }
    }

Hope this helps.

Upvotes: 0

Related Questions