Tôi muốn liệt kê tất cả dữ liệu cảm biến thô sẵn có trong Bản ghi nhớ dành cho Android.Liệt kê dữ liệu cảm biến thô trong Memo
Mã sau đây đã hoạt động trong những năm qua nhưng không hoạt động với XE8. Có thể có lỗi trình biên dịch nội bộ. Có bất cứ điều gì tôi có thể làm để làm cho nó hoạt động trở lại, hoặc là có một giải pháp thay thế?
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;
Cập nhật
Một số thí nghiệm:
(1) Khi tôi nhận xét ra là người đầu tiên "cho", nó sẽ biên dịch:
// 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) Khi tôi nhận xét việc gán "n" và "v", nó cũng sẽ biên dịch:
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;
Vì không phải "cho", cũng không "n" và "v" là khu vực không tốt, đó là lỗi ở đâu?
(3) Khi tôi nhận xét vòng lặp thứ hai, nó sẽ biên dịch lại. Nếu tôi bình luận ra đầu tiên cho vòng lặp, nó sẽ biên dịch quá. Mỗi công trình lặp lại, nhưng kết hợp chúng sẽ không hoạt động.
Dường như lỗi chỉ xảy ra nếu 5 yếu tố được kết hợp:
- TypInfo
- Accessors
- vòng lặp for
- Sử dụng TypInfo (GetEnumName)
- Cả hai cho-vòng được sử dụng.
Cập nhật 2
Đây là mã tái sản xuất nhỏ nhất tôi có thể tìm thấy. Nếu bất kỳ dòng nào được nhận xét, nó biên dịch:
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.
Bạn có thể giải thích thêm một chút nữa không? Tôi đang cố gắng tìm ra nó từ nhiều giờ. 'p_orientation' là' TCustomOrientationSensor' và phía bên kia. Khi tôi xóa những thứ không liên quan như "TypInfo", nó hoạt động trở lại. –
Tuyên bố đó mâu thuẫn với những gì bạn báo cáo trong câu hỏi. –
Tuyên bố của tôi là, nó không biên dịch nữa. Bạn có thể tự mình kiểm tra. Tôi nhận xét ra một số điều, nhưng tôi không thể tìm ra nơi mà vấn đề là, vì vậy tôi giả định một lỗi trình biên dịch. –