MessageBox & Dialogs on XNA (C#)

若如初见. 提交于 2019-12-23 04:57:26

问题


I'm having the following code as part of my XNA game:

    private void gameOver()
    {
        if (!m_IsGameOver)
        {
            string message = String.Format("GAME OVER!!!{0}Your score is: {1}",
                        Environment.NewLine, GameScore);
            if (MessageBox.Show(message, "GameOver", MessageBoxButtons.OK) == DialogResult.OK)
            {
                this.Exit();
            }

            m_IsGameOver = true;
        }
    }

    private void gameWon()
    {
        string message = String.Format("You Won!!!{0}Your score is: {1}",
                    Environment.NewLine, GameScore);
        if (MessageBox.Show(message, "You Won!", MessageBoxButtons.OK) == DialogResult.OK)
        {
            this.Exit();
        }
    }       

For some reason,I received the following errors:

"The name 'MessageBox' does not exist in the current context"  
"The name 'MessageBoxButtons' does not exist in the current context"    
"The name 'DialogResult' does not exist in the current context"  

I'm tring to add "System.Windows..." but it seems like that "System" does not have "windows" in it...

How can I solve this?


回答1:


It seems that you are trying to use WinForms classes in XNA. However, according to the docs, WinForms is not included in XNA: As can be seen in the MessageBox docs, none of the MessageBox methods has the XNA logo in the first column, which means that none of them is supported in XNA. (See, for contrast, the docs on System.Linq.Enumerable, where all methods have the X-shaped XNA logo next to them).

For in-game GUIs, various solutions such as this one exist; more links are included in this, this and this SO question and this MSDN forum posting contains another list of links.




回答2:


OK, I found the solution...
Very simple :/
Adding the "System.Windows" to the "References" section in the project :)




回答3:


Also, if you try to bring in System.Windows.Forms, and you are using XNA's keys, like Keys.Up, there will be a name conflict with System.Windows.Forms Keys.



来源:https://stackoverflow.com/questions/11815283/messagebox-dialogs-on-xna-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!