Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
News

使用TDialogService类别

在前一阵子举办的RAD Studio 10.2.2网络发表会中我提到了以前Delphi/C++Builder程序员经常使用的一些对话盒函式都已经被Embarcadero标注为过时弃用了, 例如MessageDlg, InputQueryShowMessage:

function MessageDlg(const AMessage: string; const ADialogType: TMsgDlgType; const AButtons: TMsgDlgButtons;

  const AHelpContext: THelpContext): Integer; overload; inline; deprecated ‘Use FMX.DialogService methods’;

 
如果您是使用比较新的RAD Studio版本. 那么您应该开始改用FMX.DialogService程序单元中TDialogService类别的类别方法来取代以前的MessageDlg, InputQueryShowMessage等函式.
 
这是因为现在RAD Studio支持了多个不同的平台, 而不同平台对于对话盒的支持不同, 例如有的平台支持同步和异步对话盒, 而有的平台只支持同步对话盒. TDialogService类别很方便的统一了不同平台对于对话盒实作的差异. TDialogService类别提供了PreferredMode特性可以让您指定使用同步或异步对话盒, 如果您不确定您使用的平台的对话盒模式, 那么PreferredMode特性值在内定上是使用执行平台模式, 您就不用烦恼了.
 
TDialogService类别中不但提供了MessageDlg, InputQueryShowMessage等类别方法来取代传统的MessageDlg, InputQueryShowMessage等方法, 更有提供进阶的功能. 例如在TDialogService类别中提了3种覆载的ShowMessage方法:

    class procedure ShowMessage(const AMessage: string); overload;

    class procedure ShowMessage(const AMessage: string; const ACloseDialogProc: TInputCloseDialogProc); overload;

    class procedure ShowMessage(const AMessage: string; const ACloseDialogEvent: TInputCloseDialogEvent;

      const AContext: TObject = nil); overload;

除了取代传统的ShowMessage方法, 您也可以实作程序或是事件在用户关闭ShowMessage对话盒时直接触发您的程序或是事件.例如我们使用如下的一个小范例:
 

procedure TForm2.btnShowMessageClick(Sender: TObject);

begin

  TDialogService.ShowMessage(‘您点选了OK按钮‘, ShowMessageCloseMethod);

end;

 

procedure TForm2.ShowMessageCloseMethod(const AResult: TModalResult);

var

  alvi : TListViewItem;

begin

  alvi := ListView1.Items.Add;

  alvi.Text := DateTimeToStr(Now);

  alvi.Detail := ‘关闭了ShowMessage对话盒!’;

end;

 

 

在呼叫TDialogService.ShowMessage时我们提了一个回叫方法ShowMessageCloseMethod(), 那么当使用者关闭ShowMessage对话盒时就会自动呼叫ShowMessageCloseMethod()方法:
 
TDialogService类别提供的对话盒服务可以保证在不同平台使用最适当的方式显示各种对话盒, 因此请记得从现在开始就使用TDialogService. ShowMessage, TDialogService.MessageDialogTDialogService. InputQuery等对话盒类别函式吧.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES