【问题标题】:Why isn't InputQuery returning bool?为什么 InputQuery 不返回布尔值?
【发布时间】:2020-03-21 08:32:47
【问题描述】:

我已经按照clear documentation 实现了一个输入对话框。它工作正常。但是,现在我想在用户单击取消时忽略用户输入。以下是该文档的引用。

“如果用户点击OK按钮,InputQuery返回True;InputQuery返回False否则。”

所以,我尝试了以下代码,当我在 Win32 上运行时得到的错误是 E2034 Cannot convert void to bool,在 Android 上运行时是 bccaarm error 1711 value of type void is not contextually convertible to bool。

if (InputQuery(caption, Prompts, sizeof(Prompts)/sizeof(Prompts[0]) - 1, Defaults, sizeof(Defaults)/sizeof(Defaults[0]) - 1, (TInputCloseQueryProc *)Met)){
   // clicked OK
} else {
   // clicked cancel
}

如何测试OK 或Cancel 是否被点击?下面是InputQuery 的声明,它应该是布尔值。我很困惑。

extern DELPHI_PACKAGE bool __fastcall InputQuery _DEPRECATED_ATTRIBUTE1("Use FMX.DialogService methods") (const System::UnicodeString ACaption, const System::UnicodeString *APrompts, const int APrompts_High, System::UnicodeString *AValues, const int AValues_High, const _di_TInputCloseQueryFunc ACloseQueryFunc = _di_TInputCloseQueryFunc())/* overload */;

【问题讨论】:

    标签: firemonkey c++builder c++builder-10.3-rio


    【解决方案1】:

    在InputQuery() 的最后一个参数中,您传入了TInputCloseQueryProc,但您引用的声明采用了TInputCloseQueryFunc。

    根据您链接到的documentation,采用TInputCloseQueryProc 的InputQuery() 的重载返回void,而不是bool,因此会出现转换错误。返回bool 并接受关闭回调的重载采用TInputCloseQueryFunc 或TInputCloseQueryEvent。因此,您需要相应地更新您的 Met 变量。

    话虽如此,Fmx::Dialogs::InputQuery() 函数/过程已被弃用,正如您引用的声明中清楚显示的那样。正如弃用消息所说,您应该使用InputQuery() 的Fmx::DialogService 版本。根据需要使用TDialogServiceSync::InputQuery() 或TDialogServiceAsync::InputQuery() 1。

    1:Android不支持模态对话框,所以不能在Android上使用InputQuery()的同步版本。


    附带说明,C++Builder 在<sysopen.h> 中有一个EXISTINGARRAY() 辅助宏,用于传递一个 静态数组,其中 Delphi 风格的开放数组采取,因此您不必手动指定数组边界,例如:

    InputQuery(..., EXISTINGARRAY(Prompts), EXISTINGARRAY(Defaults), ...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多