【问题标题】:MessageBox & Dialogs on XNA (C#)XNA 上的消息框和对话框 (C#)
【发布时间】:2012-08-02 15:45:51
【问题描述】:

我的 XNA 游戏中有以下代码:

    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();
        }
    }       

由于某种原因,我收到以下错误:

"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"  

我正在尝试添加“System.Windows...”,但似乎“System”中没有“windows”...

我该如何解决这个问题?

【问题讨论】:

  • 我不知道,我现在才这样做 :)
  • 为什么要混合使用 XNA 和 WinForms 库?您应该通过游戏界面显示消息,而不是使用 MessageBox。

标签: c# xna xna-4.0 messagebox dialogresult


【解决方案1】:

好的,我找到了解决方案...
很简单:/
将“System.Windows”添加到项目的“参考”部分:)

【讨论】:

  • 据我所知,例如this sitethis site,Xbox 等非 PC XNA 环境不支持 WinForms。这意味着,如果您引用 WinForms,您的游戏将不会在那里运行。另请注意,System.Windows.Forms.MessageBox(您试图在问题中使用)位于System.Windows.Forms.dll
  • 它可以工作,但前提是您没有计划将其移植到其他非 PC 平台。
【解决方案2】:

您似乎正在尝试在 XNA 中使用 WinForms 类。但是,根据文档,WinForms 不包含在 XNA 中:正如在 MessageBox docs 中所见,MessageBox 方法的第一列都没有 XNA 徽标,这意味着 XNA 中不支持它们. (相比之下,请参见docs on System.Linq.Enumerable,其中所有方法旁边都有 X 形的 XNA 徽标。

对于游戏内的GUI,存在this one等多种解决方案; thisthisthis SO question 中包含更多链接,this MSDN forum posting 包含另一个链接列表。

【讨论】:

    【解决方案3】:

    另外,如果您尝试引入 System.Windows.Forms,并且您正在使用 XNA 的密钥,例如 Keys.Up,则会与 System.Windows.Forms 密钥发生名称冲突。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      相关资源
      最近更新 更多