我有一個正在移植到FireMonkey的VCL應用程序。我遇到的其中一個問題是FireMonkey中不推薦使用MessageDlg(...)
。進一步挖掘,我明白我必須使用FMX.DialogService.MessageDialog
方法。所以,我創建了一個函數來顯示一個對話框:德爾福 - 在FireMonkey中正確顯示消息對話框並返回模態結果
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;
當我運行它,顯示的消息對話框是這樣的:
它不顯示2個按鈕(是的,取消) 。有人可以幫我解決這個問題 - 即用2個按鈕正確顯示消息對話框,並將消息對話框的模態結果作爲功能的結果發回。
我正在使用Delphi 10.1柏林更新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;
您可以在匿名方法中設置局部變量,以便該位正常。其餘的我都看不出有什麼問題。 – Dsm