自定义消息框显示在消息框之后

Custom Message Box displays after Message box

我有一些代码,当我调用 CustomMessageBox 时,它会显示一个框,提示用户输入要添加的对象数量,完成后我将其添加到对象列表中。添加后,它会显示 MessageBox.Show 以让用户知道它已添加。

我的问题是,当我 运行 代码时,它会执行所有代码,绕过自定义消息框的显示,然后显示 MessageBox.Show,然后显示 CMB.Show.我通过调试器 运行 代码并跟踪跟踪,它在 MessageBox.Show 之前命中 CMB.Show,但在代码完成后显示。对不起,我还在学习,可能没有很好地说明问题,如果有什么我可以进一步解释的,请告诉我。

一些代码:

    private int BasicLand(Card basicLand)
    {
        var countBox = new TextBox
        {
            Name = "count",
            Width = 100,
        };


        var cmbCount = new CustomMessageBox
        {
            Caption = "Blah",
            Content = countBox,
            RightButtonContent = "ok",
        };

        cmbCount.Dismissed += (s1, e1) =>
        {
            switch (e1.Result)
            {
                case CustomMessageBoxResult.RightButton:
                    if (int.TryParse(countBox.Text, out tempInt) && Convert.ToInt32(countBox.Text) > 0)
                    {
                        countReturn = Convert.ToInt32(tempInt);
                        break;
                    }
                    else
                    {
                        //Some code for error....
                    }
            }
        };

        cmbCount.Show();

    return countReturn;
    }

然后是代码块中最先触发但最后触发的另一部分。

    MessageBox.Show("Object was added to List!");

我尝试将 ShowDialog 添加到自定义框,但它在 VS 中出现故障。 BasicLand 在另一个方法中调用,当对象添加到列表时,它将显示 MessageBox.Show。

您的代码存在问题,它没有考虑到任何用户交互都是异步的。当你调用 Show() 它实际上会显示消息框,但它不会阻塞你当前的 运行 线程,调用 Show() 之后的其他语句将立即执行,因此你的方法 returns 用户未提供但只是默认值的返回值。要解决此问题,您必须连续编写代码。

private void PromtUserForFeeblefezerAmount(Action<int> continueFeeblefzing, Action cancel)
{
    var messagebox = CreateFeeblefezerPromt();
    messagebox.Dismissed += (sender, args) =>
    {
        if ( args.Result == CustomMessageBoxResult.RightButton )
            continueFeeblefzing( GetFeeblefezerAmount(messagebox) );
        else
            cancel();
    };
    messagebox.Show();
}