现代UI对话框结果问题

3

我正在使用Modern UI并尝试制作一个对话框,询问问题并等待回答。我可以使用messagebox实现这一点,但想尝试使用Modern UI。我不确定如何获取按钮的点击值。

if (testapp.linkvalue != "NULL")
{
    var v = new ModernDialog
    {
        Title = "my test",
        Content = "pewpew lazers rule. If you agree click ok"
    };
    v.Buttons = new Button[] { v.OkButton, v.CancelButton };
    var r = v.ShowDialog();
    if (????????????????)
    {
        MessageBox.Show("ok was clicked");
    }
    else
    {
        MessageBox.Show("cancel was clicked");
    }
}
3个回答

5
if (testapp.linkvalue != "NULL")
{
    var v = new ModernDialog
    {
        Title = "my test",
        Content = "pewpew lazers rule. If you agree click ok"
    };
v.OkButton.Click += new RoutedEventHandler(OkButton_Click);
    v.Buttons = new Button[] { v.OkButton, v.CancelButton };
    var r = v.ShowDialog();

}

//And Then Create OkButtonClick

private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("ok was clicked");
        }

做得非常好。谢谢你,我之前这个星期已经放弃希望了。 - Shane Superfly MacNeill

2
可能还有另一种解决方案,可以使用扩展方法。
var r = v.ShowDialogOKCancel();
if (r==MessageBoxResult.OK)
{
    MessageBox.Show("ok was clicked");
}
else
{
    MessageBox.Show("cancel was clicked");
}



static class ModernDialogExtension
{
    static MessageBoxResult result;

    public static MessageBoxResult ShowDialogOKCancel(this FirstFloor.ModernUI.Windows.Controls.ModernDialog modernDialog)
    {
        result = MessageBoxResult.Cancel;

        modernDialog.OkButton.Click += new RoutedEventHandler(OkButton_Click);
        modernDialog.Buttons = new Button[] { modernDialog.OkButton, modernDialog.CloseButton };

        modernDialog.ShowDialog();

        return result;
    }

    private static void OkButton_Click(object sender, RoutedEventArgs e)
    {
        result = MessageBoxResult.OK;
    }
}

2
private void CommonDialog_Click(object sender, RoutedEventArgs e)
    {
        var dlg = new ModernDialog {
            Title = "Common dialog",
            Content = new LoremIpsum()
        };
        dlg.Buttons = new Button[] { dlg.OkButton, dlg.CancelButton};
        dlg.ShowDialog();

        this.dialogResult.Text = dlg.DialogResult.HasValue ? dlg.DialogResult.ToString() : "<null>";
        this.dialogMessageBoxResult.Text = dlg.MessageBoxResult.ToString();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接