我正在尝试将代码从wpf重现为winforms(此代码在wpf中运行)
public static bool? ShowSettingsDialogFor(ICustomCustomer)
{
if (cust is BasicCustomer)
{
return (new BCustomerSettingsDialog()).ShowDialog();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误消息
无法将类型'System.Windows.Forms.DialogResult'隐式转换为'bool?'
Ant*_*ula 11
改变为
return (new BCustomerSettingsDialog()).ShowDialog() == DialogResult.OK;
Run Code Online (Sandbox Code Playgroud)
原因是,在Windows窗体中,该ShowDialog方法返回DialogResult枚举值.可能值的范围取决于您可用的按钮,它们的bool?转换可能取决于它们在您的应用程序中的含义.以下是处理一些情况的一些通用逻辑:
public static bool? ShowSettingsDialogFor(ICustomCustomer)
{
if (cust is BasicCustomer)
{
DialogResult result = (new BCustomerSettingsDialog()).ShowDialog();
switch (result)
{
case DialogResult.OK:
case DialogResult.Yes:
return true;
case DialogResult.No:
case DialogResult.Abort:
return false;
case DialogResult.None:
case DialogResult.Cancel:
return null;
default:
throw new ApplicationException("Unexpected dialog result.")
}
}
}
Run Code Online (Sandbox Code Playgroud)