Jak*_*ays 1 delphi android ios firemonkey delphi-xe5
如果这已经在SO上访问了,请指出我,因为我似乎无法找到它.话说回来:
使用标准的delphi应用程序事件,以及移动应用程序生命周期事件处理,我试图找到读取和写入INI文件的最佳位置?
当我测试时,我创建了一个带有按钮的演示应用程序,该按钮递增计数变量并将其显示在显示消息中
procedure TfrmMain.Button1Click(Sender: TObject);
begin
inc(Count);
ShowMessage(IntToStr(Count));
end;
Run Code Online (Sandbox Code Playgroud)
在主要形式的OnCreate甚至,我读了inifile
procedure TfrmMain.FormCreate(Sender: TObject);
var
Ini: TIniFile;
begin
Ini := TIniFile.Create( TPath.GetDocumentsPath + PathDelim + 'fortysixtozero.ini' );
try
Count := Ini.ReadInteger( 'Main', 'Count', 0 );
finally
Ini.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)
现在,知道移动应用程序可以有不同的状态,我想知道编写ini文件的最佳位置在哪里?
保存应用程序状态或存储设置的最佳状态是" aeEnteredBackground ".我在这里使用了delphi FMX事件.您还应该检查" aeWillBecomeInactive "和" aeWillTerminate "事件,但第一个事件是最相关的事件.当另一个应用程序打开或您的应用程序关闭时,应用程序进入后台(它们不会立即终止).
看看这篇文章.
侦听事件的代码如下所示:
function TfMain.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
case AAppEvent of
aeFinishedLaunching: ;
aeBecameActive: ;
aeWillBecomeInactive: ;
aeEnteredBackground: ;
aeWillBecomeForeground: ;
aeWillTerminate: ;
aeLowMemory: ;
aeTimeChange: ;
aeOpenURL: ;
end;
Result := True;
end;
Run Code Online (Sandbox Code Playgroud)
要附加侦听器,请使用平台服务:
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(SvcEvents)) then
SvcEvents.SetApplicationEventHandler(HandleAppEvent);
Run Code Online (Sandbox Code Playgroud)
只需在您的uses子句中添加"FMX.Platform"即可.
小智 5
在 Delphi XE7 中,表单有一个“OnSaveState”事件。这是保存应用程序数据的首选位置,因为它会在 iOS 应用程序进入“后台”状态时执行。该文档非常有用......搜索“保存状态”。
这是我在主窗体的 OnCreate 处理程序中的代码:
procedure TMainWindow.FormCreate( Sender : TObject );
var
IniFile : TIniFile;
Metric : BOOLEAN;
IniFileName : STRING;
Reader : TBinaryReader;
begin
fInitializing := True;
SaveState.StoragePath := TPath.GetLibraryPath;
if SaveState.Stream.Size > 0 then begin
Reader := TBinaryReader.Create( SaveState.Stream );
try
Metric := Reader.ReadBoolean;
vMetricUnits.IsChecked := Metric;
SetSliderLimits( Metric );
Temperature := Reader.ReadDouble;
Dewpoint := Reader.ReadDouble;
Humidity := Reader.ReadDouble;
WindSpeed := Reader.ReadDouble;
finally
Reader.Free;
end;
end
else begin
Metric := False;
vMetricUnits.IsChecked := Metric;
SetSliderLimits( Metric );
Temperature := 70;
Dewpoint := 70;
Humidity := 100;
WindSpeed := 0;
end;
SetMetricUnits( cMetricUnits );
fInitializing := False;
WriteTrackbarCaptions;
CalculateTemperatures;
end;
Run Code Online (Sandbox Code Playgroud)
这是表单的 OnSaveState 处理程序中的代码:
procedure TMainWindow.FormSaveState( Sender : TObject );
var
Writer : TBinaryWriter;
begin
SaveState.Stream.Clear;
Writer := TBinaryWriter.Create( SaveState.Stream );
try
Writer.Write( cMetricUnits );
Writer.Write( Temperature );
Writer.Write( Dewpoint );
Writer.Write( Humidity );
Writer.Write( WindSpeed );
finally
Writer.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)
我已经在 iPad 和 Windows 上对此进行了测试,并且可以在两个平台上运行。这样做完全避免了 .ini 文件的使用,但是它确实在 Windows 版本中创建了一个有点奇怪的 .tmp 文件。我假设在 iPad 上也创建了一个等效的文件。
| 归档时间: |
|
| 查看次数: |
11538 次 |
| 最近记录: |