这是我的 C# 代码。我在该特定行中遇到错误,因为运算符“&&”无法应用于“bool?”类型的操作数和“ bool ”
if (openFileDialog.ShowDialog() && viewModel.OpenFileCommand.CanExecute(openFileDialog.FileName))
{
viewModel.OpenFileCommand.Execute(openFileDialog.FileName);
}
请帮我纠正这个错误。
最佳答案
Returns
Nullable<Boolean>
A Nullable value of type Boolean that specifies whether the activity was accepted (true) or canceled (false). The return value is the value of the DialogResult property before a window closes.
问题是 ShowDialog
的结果可以为空。
但是,您可以通过使用更详细的 == true
来纠正此问题。 ,编译器就会明白您想要明确地知道它是 true,而不是 null 或 false。不要问我为什么它不喜欢速记版本(我必须深入研究规范)。
无论哪种方式,这都是您的解决方案:
if (openFileDialog.ShowDialog() == true && viewModel....)
关于c# - 如何纠正错误运算符 '&&' 无法应用于类型 'bool?' 和 'bool' 的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54032722/