Windows Phone 7 Development tutorial (3)-use MessageBox and soft keyboard in xNa

Source: Internet
Author: User
Document directory
  • Call MessageBox
  • Call the software Input Panel
  • Written to the end

Author: Ma Ning

I believe that I will join xNa in my spare time in the future. I wanted to develop in depth to 3D development, but I encountered a practical problem, that is, how to display MessageBox and software Input Panel under xNa. Simply write it out first, saving everyone crazy when encountering this problem.

According to a few public documents, xNa and Silverlight for Windows Phone should be based on the same. NET Compact framework CLR. However, xNa does not provide any user controls, MessageBox, and keypad, nor can it directly call the Silverlight for Windows Phone class libraries. This will inevitably create many artificial obstacles for xNa. However, xNa left us with the backdoor that calls MessageBox and software Input Panel. This is the one in the namespace of Microsoft. xNa. Framework. gamerservices.GuideClass:

Http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gamerservices.guide.aspx

This class can not only call MessageBox and soft keyboard, but also call forms such as marketplace and Xbox Live. However, it is worth noting that the methods provided by the guide class are asynchronous calls rather than synchronous calls. It is also understandable that the game's processing process is time-driven, therefore, no operation should block the main game thread.

Here is the description of the guide class call MessageBox and the soft keyboard:

Http://msdn.microsoft.com/en-us/library/ff827869.aspx

Http://msdn.microsoft.com/en-us/library/ff827868.aspx

However, there are still some flaws in the msdn document. Exceptions are generated according to the methods provided above, so I will provide the methods that can be run after modification below. The running environment is based on vs 2010 + Windows Phone 7 sdk rtw.

Call MessageBox

Create an xNa 4.0 Project in Windows Phone 7, and then add a call to MessageBox in the update method. Of course, do not consider that adding MessageBox to update is correct. This will cause MessageBox to pop up continuously. I did this only to simplify the code.

protected override void Update (GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState (PlayerIndex.One) .Buttons.Back == ButtonState.Pressed)
                this.Exit ();

            // TODO: Add your update logic here
            List <string> MBOPTIONS = new List <string> ();
            MBOPTIONS.Add ("OK");
            MBOPTIONS.Add ("CANCEL");
            if (! Guide.IsVisible)
                Guide.BeginShowMessageBox ("test", "hello, XNA", MBOPTIONS, 0, MessageBoxIcon.Alert, new AsyncCallback (RespCallback), null);

            base.Update (gameTime);
        }
Since Microsoft.Xna.Framework.GamerServices is a component added by default, we can directly use the Guide class. First create a String type List to save the Text of the button in the MessageBox; then add the Button Text that needs to be displayed through the Add method of List <string>.

Next is the Guide.IsVisible method. Because MessageBox, SIP soft keyboard, etc. share a drawing surface, you must ensure that there is no other UI display before you can display the specified component. If you do not add the sentence Guide.IsVisible, the following exception will be triggered .

Next is the main body of the call Guide.BeginShowMessageBox. This is an asynchronous method and returns immediately after the call. The parameters are easier to understand. The following is the function declaration:

public static IAsyncResult BeginShowMessageBox (
         string title,
         string text,
         IEnumerable <string> buttons,
         int focusButton,
         MessageBoxIcon icon,
         AsyncCallback callback,
         Object state
)
The first parameter is the title, the second parameter is the content of the dialog box, the third is the list of text on the button, which also indicates how many Buttons appear, the fourth is the focus on the few Buttons, the fifth is Icon, we set to null, the sixth is the Callback function object called at the end, and the last one is the user-defined state object, which can pass custom information. Other parameters are easy to understand. The AsyncCallback object requires a Callback function RespCallback, which we implement as follows:

        private static void RespCallback (IAsyncResult asynchronousResult)
        {
            int? b = Guide.EndShowMessageBox (asynchronousResult);
            if (b> 0)
                Debug.WriteLine ("Cancel");
            else
                Debug.WriteLine ("OK");
        }
The most important job in the Callback function is to call the Guide.EndShowMessageBox function to close the MessageBox. EndShowMessageBox needs to pass in an IAsyncResult object, parameters from the Callback function. The return value is a nullable int. If it is empty, it means there is no return value. If it is not empty, the return value is the Index value of the Button. A return value of 0 means that the first button was clicked. The two buttons Cancel, and so on.

The effect of displaying MessageBox is as follows:

Call Software Input Panel
Next is the code to call the SIP soft keyboard, which is still put in the Update method, and the Callback function is also given.

        protected override void Update (GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState (PlayerIndex.One) .Buttons.Back == ButtonState.Pressed)
                this.Exit ();


            if (! Guide.IsVisible)
                Guide.BeginShowKeyboardInput (PlayerIndex.One,
                        "Here's your Keyboard", "Type something ...",
                        "abc",
                        new AsyncCallback (GetTypedChars),
                        null);

            base.Update (gameTime);
        }

        private static void GetTypedChars (IAsyncResult asynchronousResult)
        {
            string output = Guide.EndShowKeyboardInput (asynchronousResult);
            Debug.WriteLine (output);
        }
As explained above, let ’s look directly at Guide.BeginShowKeyboardInput. The first parameter is to pass PlayerIndex in. This is for Xbox. Only one user is supported on Windows and Windows Phone 7. So just pass PlayerIndex.One directly. The next three parameters are the title, description, and default characters, followed by the asynchronous call method and custom status. There is also a final optional parameter that indicates whether to display characters in Password mode.

public static IAsyncResult BeginShowKeyboardInput (
         PlayerIndex player,
         string title,
         string description,
         string defaultText,
         AsyncCallback callback,
         Object state,
         bool usePasswordMode
)
In the asynchronous call method, Guide.EndShowKeyboardInput will return a string, which is the string input by the user. Why is the string displayed? This is related to the way SIP is displayed. After calling the SIP function, the first dialog box will pop up first, asking the user whether to enter characters. If the user clicks Cancel to close the SIP, if clicks OK to enter the second interface, the user can use the SIP soft keyboard for input.

The following is the status of SIP display:

Write to the end
Today's protagon Guide class, there are many interesting function calls, friends who are interested can call according to this method. This time the amount of code is not much, so I will not give a separate Sample Code download. Another thing is to accept criticism humbly and get the format of the code in the article right.

I have been very diligent recently. I have written three articles on the development of Windows Phone 7. This is the link to the previous article:

Maning's Windows Phone 7 development tutorial (1)-the first experience of Windows Phone development tools

Maning's Windows Phone 7 development tutorial (2)-Windows Phone XNA 4.0 3D game development

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.