在备忘录中列出原始传感器数据

时间:2015-04-21 12:34:11

标签: android delphi firemonkey delphi-xe8

我想在Memo for Android中列出所有可用的原始传感器数据。

以下代码在过去几年中起作用,但它不适用于XE8。可能存在内部编译器错误。我能做些什么才能让它再次发挥作用,还是有替代解决方案?

uses
  TypInfo;

type
  TOrientationSensorAccessor = class(TCustomOrientationSensor);
  TLocationSensorAccessor = class(TCustomLocationSensor);

procedure TForm2.Button1Click(Sender: TObject);
var
  p_location: TCustomLocationSensor.TProperty;
  p_orientation: TCustomOrientationSensor.TProperty;
  n, v: string;
begin
  Memo1.Lines.Clear;

  if Assigned(OrientationSensor1.Sensor) then
  begin
    if not OrientationSensor1.Sensor.Started then OrientationSensor1.Sensor.Start;

    // Error (only in XE8): Incompatible types 'TCustomLocationSensor.TProperty' and 'TCustomOrientationSensor.TProperty'
    // In XE7 it works.
    for p_orientation in OrientationSensor1.Sensor.AvailableProperties do
    begin
      n := 'OrientationSensor.'+GetEnumName(TypeInfo(TCustomOrientationSensor.TProperty), integer(p_orientation)) ;
      v := FloatToStr(TOrientationSensorAccessor(OrientationSensor1.Sensor).GetDoubleProperty(p_orientation));
      Memo1.Lines.Values[n] := v;
    end;
  end;

  if Assigned(LocationSensor1.Sensor) then
  begin
    if not LocationSensor1.Sensor.Started then LocationSensor1.Sensor.Start;
    for p_location in LocationSensor1.Sensor.AvailableProperties do
    begin
      n := 'LocationSensor.'+GetEnumName(TypeInfo(TCustomLocationSensor.TProperty), integer(p_location)) ;
      v := FloatToStr(TLocationSensorAccessor(LocationSensor1.Sensor).GetDoubleProperty(p_location));
      Memo1.Lines.Values[n] := v;
    end;
  end;
end;

更新

一些实验:

(1)当我注释掉第一个" for"时,它会编译:

//    for p_orientation in OrientationSensor1.Sensor.AvailableProperties do
//    begin
      n := 'OrientationSensor.'+GetEnumName(TypeInfo(TCustomOrientationSensor.TProperty), integer(p_orientation)) ;
      v := FloatToStr(TOrientationSensorAccessor(OrientationSensor1.Sensor).GetDoubleProperty(p_orientation));
      Memo1.Lines.Values[n] := v;
//    end;
  end;

(2)当我注释掉" n"和" v",它也会编译:

    for p_orientation in OrientationSensor1.Sensor.AvailableProperties do
    begin
//      n := 'OrientationSensor.'+GetEnumName(TypeInfo(TCustomOrientationSensor.TProperty), integer(p_orientation)) ;
//      v := FloatToStr(TOrientationSensorAccessor(OrientationSensor1.Sensor).GetDoubleProperty(p_orientation));
//      Memo1.Lines.Values[n] := v;
    end;
  end;

既不是#34;也不是" n"和" v"是坏区域,那么错误在哪里?

(3)当我注释掉第二个for循环时,它会再次编译。如果我注释掉第一个for循环,它也会编译。每个for循环都可以工作,但是它们组合起来不起作用。

看起来错误只发生在5个因素的组合中:

  • TypInfo
  • 访问者
  • for loop
  • 使用TypInfo(GetEnumName)
  • 使用两个for循环。

更新2

这是我能找到的最小的可重现代码。如果任何行被注释掉,它将编译:

program ProjectCompilerBug;

{$APPTYPE CONSOLE}

uses
  System.Sensors, System.Sensors.Components;

var
  p_location: TCustomLocationSensor.TProperty;
  p_orientation: TCustomOrientationSensor.TProperty;
begin
  // Compilation Error (only in XE8):
  // "Incompatible types 'TCustomLocationSensor.TProperty' and 'TCustomOrientationSensor.TProperty'"
  // In XE7 it compiles
  for p_orientation in TOrientationSensor.Create(nil).Sensor.AvailableProperties do
  begin
    FloatToStr(1.23);
  end;

  for p_location in TLocationSensor.Create(nil).Sensor.AvailableProperties do
  begin
  end;
end.

1 个答案:

答案 0 :(得分:5)

是的,这看起来像是一个XE8编译器错误。我认为你做得很好,隔离它,我赞赏你。您需要向Quality Portal提交错误报告。

要解决这个错误,我认为你可以将循环放在不同的函数中。我的假设是关键是存在两个for循环,其中不同类型的循环变量是关键。避免这种情况,你应该能够避免这个问题。