在启动 XNA 游戏之前启动一个表单

Start a form before launching an XNA Game

所以我用浏览器创建了一个表单,可以访问我游戏网站上的 "News" 页面。它有 2 个按钮:"Start Game" 和 "Level Editor"。

我目前正在使用 DialogResult 检查是否有任何按钮被点击 - "Start Game" 有 DialogResult = Yes 而 "Level Editor" 有 DialogResult = No。是的,这是一个丑陋的解决方案,但我无法使其与 Application.Run(newsForm); 一起使用,因为我不知道如何检查 newsForm class.

之外的按钮点击 我的 XNA 项目的

Program.cs:

[STAThread]
private static void Main()
{
    Application.EnableVisualStyles();
    using (var newsForm = new NewsForm())
    {
        if (newsForm.ShowDialog() == DialogResult.Yes)
        {
            using (var game = new Game1())
            {
                game.Run();
            }
            newsForm.Dispose();
        }
        else if (newsForm.ShowDialog() == DialogResult.No)
        {
            using (var editor = new EditorForm())
            {
                Application.Run(editor);
                newsForm.Dispose();
            }
        }
    }
}

在我的 NewsForm 设计器中,我将按钮的 DialogResult 属性 设置为适当的值。但我还有一个问题:

当我单击 "Start Game" 时,XNA 窗体正确显示并且 NewsForm 关闭。但是当我点击 "Level Editor" 时,表格看起来像是关闭然后重新打开......我必须再次点击按钮才能启动 editor 表格。如果我点击 "Start Game" 它会关闭。我确定这是由 DialogResult.

引起的

正如我上面所说,我这样做的方式在设计上绝对不是正确的,因为:

  1. 这是一个表单,而不是对话框
  2. 按钮的含义与 DialogResult 或用户期望的不同

因此,如果您知道我如何实现上述目标,有或没有(最好)ShowDialog,我将不胜感激。

使用switch()代替if():

[STAThread]
private static void Main()
{
    Application.EnableVisualStyles();
    using (var newsForm = new NewsForm())
    {
        DialogResult dr = newsForm.ShowDialog();
        switch (dr)
        {
            case DialogResult.Yes:
                using (var game = new Game1())
                {
                    game.Run();
                    newsForm.Close();
                    newsForm.Dispose(); // since you open the form with ShowDialog(), you must dispose of it manually.
                }
                break;
            case DialogResult.No:
                using (var editor = new EditorForm())
                {
                    Application.Run(editor);
                    newsForm.Close();
                    newsForm.Dispose(); // since you open the form with ShowDialog(), you must dispose of it manually.
                }
                break;
        }
    }
}