Delphi – 在FireMonkey中正确显示消息对话框并返回模态结果

我有一个VCL应用程序,我移植到FireMonkey.我遇到的一件事是在FireMonkey中不推荐使用MessageDlg(…).在进一步挖掘时,我知道我必须使用FMX.DialogService.MessageDialog方法.所以我创建了一个显示对话框的函数:

function TfMain.GetDeleteConfirmation(AMessage: String): String;
var
  lResult: String;
begin
  lResult:='';
  TDialogService.PreferredMode:=TDialogService.TPreferredMode.Platform;
  TDialogService.MessageDialog(AMessage, TMsgDlgType.mtConfirmation,
    [ TMsgDlgBtn.mbYes, TMsgDlgBtn.mbCancel ], TMsgDlgBtn.mbCancel, 0,
    procedure(const AResult: TModalResult)
    begin
      case AResult of
        mrYes:    lResult:='Y';
        mrCancel: lResult:='C';
      end;
    end);

  Result:=lResult;
end;

我不认为我这样做是正确的,因为我不确定我可以在匿名方法中设置局部变量,但它仍然编译.

我称之为:

  if GetDeleteConfirmation('Are you sure you want to delete this entry?')<>'Y' then
    exit;

当我运行它时,显示的消息对话框是这样的:

《Delphi – 在FireMonkey中正确显示消息对话框并返回模态结果》

它不显示2个按钮(是,取消).有人可以帮我解决这个问题 – 即正确显示带有2个按钮的消息对话框,然后将消息对话框的模态结果作为功能结果发回.

我正在使用Delphi 10.1 Berlin Update 2.

提前谢谢了!

编辑20170320:我在下面的@LURD的正确答案的基础上更正了我的代码,并将其包含在此为完整性:

function TfMain.GetDeleteConfirmation(AMessage: String): String;
var
  lResultStr: String;
begin
  lResultStr:='';
  TDialogService.PreferredMode:=TDialogService.TPreferredMode.Platform;
  TDialogService.MessageDialog(AMessage, TMsgDlgType.mtConfirmation,
    FMX.Dialogs.mbYesNo, TMsgDlgBtn.mbNo, 0,
    procedure(const AResult: TModalResult)
    begin
      case AResult of
        mrYes: lResultStr:='Y';
        mrNo:  lResultStr:='N';
      end;
    end);

  Result:=lResultStr;
end;

最佳答案 题:

It does not show the 2 buttons (Yes, Cancel). Could someone please help me get this right – i.e. correctly show the message dialog with the 2 buttons and send the modal result of the message dialog back as the Result of the function.

Fmx.TDialogService.MessageDialog不支持对话框按钮的任意组合.

查看源代码(Fmx.Dialogs.Win.pas)会显示这些有效组合(mbHelp可以包含在所有组合中):

  • mbOk
  • mbOk,mbCancel
  • mbYes,mbNo,mbCancel
  • mbYes, mbYesToAll, mbNo,
    mbNoToAll, mbCancel
  • mbAbort, mbRetry, mbIgnore
  • mbAbort, mbIgnore
  • mbYes, mbNo
  • mbAbort, mbCancel

这意味着[mbYes,mbCancel]不是有效组合,例如使用[mbOk,mbCancel].

关于Fmx.TDialogService.MessageDialog的最后一点说明.它通常是桌面应用程序上的同步对话框,但在移动平台上是异步的.根据这些条件,用例看起来会有所不同,因此对于多平台应用程序,请检查TDialogService.PreferredMode的值.

点赞