2014-02-20 40 views
3

我正在使用Modern UI并尝试创建一个对话框,询问问题然后等待响应。我可以用messagebox做到这一点,但尽管我会尝试使用现代UI。我不确定如何获得按钮的点击值。现代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"); 
    } 
} 

回答

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

是做到了完美。谢谢你,本周早​​些时候我放弃了希望。 –

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