標籤:phi 跨平台 option 調用 name 函數實現 rate 通過 代碼
這個版本的delphi對ShowMessage進行了重新實現,更好的適應移動跨平台,即在移動平台下是非同步執行的,而在Windows及os X下是同步執行的,如果自己控制是否非同步顯示對話方塊,也可以通過TDialogServiceAsync(非同步對話方塊)及TDialogServiceSync(同步對話方塊)來顯示對話方塊,如果使用這個兩個類,需要手工uses對應的單元。當然了,最簡捷的方法還是ShowMessage.下面我直接上了代碼並對三種使用方法做了注釋,分享之。 unit Unit2; interface uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.StdCtrls, FireDAC.Stan.Intf,
FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
FireDAC.Phys.Intf, FireDAC.DApt.Intf, Data.DB, FireDAC.Comp.DataSet,
FireDAC.Comp.Client, System.Rtti, FMX.Grid.Style, Data.Bind.EngExt,
Fmx.Bind.DBEngExt, Fmx.Bind.Grid, System.Bindings.Outputs, Fmx.Bind.Editors,
Data.Bind.Components, Data.Bind.Grid, Data.Bind.DBScope, FMX.ScrollBox,
FMX.Grid,FMX.DialogService.Async; // FMX.Grid,FMX.DialogService.Async實現非同步對話方塊的單元,需要手工加入. type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
procedure ShowMessageDialogCallBack(Sender: TObject; const AResult: TModalResult);
end; var //
Form2: TForm2; implementation {$R *.fmx} procedure TForm2.Button1Click(Sender: TObject);
begin
//在移動平台下非同步執行,在windows,osx上同步執行,查看源碼,會發現具體實現中判斷了是否是android
//與ios,是的話,通過TDialogServiceAsnyc這個類來顯示非同步對話方塊
ShowMessage(‘Hello world.‘);
//如果要在Windows及os X上也非同步顯示對話方塊,則需要直接用TDialogServiceAsnyc來顯示
// TDialogServiceAsync.ShowMessage(‘Hello world async.‘);
end; procedure TForm2.Button2Click(Sender: TObject);
begin
//直接顯示非同步對話方塊,並通過匿名函數實現回調,當使用者點擊確定按鈕時,執行該匿名函數
//在所有平台下測試的結果,都是非同步執行的,換句話說,在Windows及osX上也不是模態顯示。
TDialogServiceAsync.ShowMessage(‘Hello world‘,
procedure (const AResule:TModalResult)
begin
ShowMessage(‘Click ok‘);
end
);
end; procedure TForm2.Button3Click(Sender: TObject);
begin
//通過回調事先寫好的事件函數,來非同步顯示對話方塊,即當使用者按下確定按鈕時,執行
//ShowMessageDialogCallBack.
//在所有平台下測試的結果,都是非同步執行的,換句話說,在Windows及osX上也不是模態顯示。
TDialogServiceAsync.ShowMessage(‘Test‘,ShowMessageDialogCallBack,nil);
end; procedure TForm2.ShowMessageDialogCallBack(Sender: TObject;
const AResult: TModalResult);
begin
showMessage(‘Call Back.‘);
end; end.
後記,進一步看了MessageDialog的實現,基本同TDialogServiceAsync.ShowMessage,只不過,可以指定顯示幾個按鈕,象Yes,No,Cancel等,官方內部實現的代碼,看起來很囉嗦,一點都不好看。試著調用這個方法,寫起來也不爽,同時在IOS模擬器,osX,Windows下運行,按鈕都是英文的,沒找到哪裡個性成中文,這就不完美了,總不能讓中文app顯示出一個帶Yes No的對話方塊吧,不論不類的。 怎麼辦呢?難怪當我說Showmessage這個實現完美的時候,高老師說不完美,還真是如此,如果想完美,還是用高師的通用介面好了! 再後記:找到解決方案,把fmx.consts複製到項目下,修改其中的資訊為漢字即可。 http://blog.sina.com.cn/s/blog_44fa172f0102w23d.html
Delphi berlin ShowMessage的改進與使用