我正在尝试创建一个对话框,用户可以在其中选择一些按钮,但我现在遇到的问题是当用户关闭窗口时(不是选择按钮,而是使用右上角的 x),应用程序显示消息,但之后它崩溃。有人知道我在这里做错了什么吗?
主窗口.xaml.cs
public partial class MainWindow : Window
{
string[,] suppliers = new string[3,2] {{"xxx", "xxx"}, {"yyy", "yyy"}, {"zzz" , "zzz"}};
public MainWindow()
{
InitializeComponent();
ButtonPrompt buttonPrompt = new ButtonPrompt(suppliers, "Select supplier.");
while (buttonPrompt.ShowDialog() != true)
{
MessageBox.Show("Please choose one of the suppliers!");
}
}
}
ButtonPrompt.xaml.cs:
public partial class ButtonPrompt : Window
{
public ButtonPrompt(string[,] buttons, string question)
{
InitializeComponent();
buttonStack.Children.Clear();
TextBlock questionBlock = new TextBlock();
questionBlock.Text = question;
buttonStack.Children.Add(questionBlock);
for (int i = 0; i < buttons.GetLength(0); i++)
{
Button inputButton = new Button();
inputButton.Name = buttons[i, 0];
inputButton.Content = buttons[i, 1];
inputButton.Width = 200;
inputButton.Height = 60;
inputButton.Click += inputButton_Click;
buttonStack.Children.Add(inputButton);
if (i == 0)
{
inputButton.Focus();
}
}
}
private void inputButton_Click(object sender, RoutedEventArgs e)
{
Button inputButton = (Button)sender;
this.DialogResult = true;
}
private void Window_Closed(object sender, EventArgs e)
{
this.DialogResult = false;
}
}
提前致谢!