我正在努力制作一个节目,并为"活动"显示可用和不可用的座位.我得到的错误如下:
'System.Nullable'不包含'Yes'的定义,并且没有扩展方法'Yes'可以找到接受类型'System.Nullable'的第一个参数(你是否缺少using指令或汇编引用?)
(同样适用于"不")和
'System.Windows.Window.DialogResult'是'属性',但用作'类型'.
这是我到目前为止:
private void btnSeat1_Click(object sender, RoutedEventArgs e) { if (!Seat1) { DialogResult Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question); if (Result == DialogResult.Yes) { MessageBox.Show("You Reserved this seat"); btnSeat1.Text = "Reserved"; } else if (Result == DialogResult.No) { Environment.Exit(0); }
注意:我只使用Environment.Exit作为占位符.这是故意的,并将相应地改变.它不是问题的根源.
这一行:
DialogResult Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);
应该是这样的:
var Result = MessageBox.Show("This Seat is Available, Would you like to pick it?", "Would you like this seat?", MessageBoxButton.YesNo, MessageBoxImage.Question);
然后,如果你将鼠标悬停在Result
你身上,你可以看到它不是a DialogResult
,而是a MessageBoxResult
,或者,如果你想明确输入它,请尝试:
MessageBoxResult Result = MessageBox.Show(...
所以你会在你的if语句中使用它:
if (Result == MessageBoxResult.Yes) { MessageBox.Show("You Reserved this seat"); btnSeat1.Text = "Reserved"; } else if (Result == MessageBoxResult.No) { Environment.Exit(0); }
你得到错误,因为DialogResult
它实际上是Window
类的属性,并且你试图像它的类型一样使用它(正如编译器所说).