2015-04-21 25 views
7

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. 
+0

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. –

+0

Tuyên bố đó mâu thuẫn với những gì bạn báo cáo trong câu hỏi. –

+0

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. –

Trả lời

5

Vâng, điều này trông giống như lỗi trình biên dịch XE8. Tôi nghĩ bạn đã làm tốt công việc cô lập nó, mà tôi khen bạn. Bạn sẽ cần gửi báo cáo lỗi cho Cổng thông tin chất lượng.

Để khắc phục lỗi, tôi nghĩ bạn sẽ có thể đặt các vòng lặp trong các chức năng riêng biệt. Giả thuyết của tôi là chìa khóa là sự hiện diện của hai trong vòng lặp với các biến vòng lặp được nhập khác nhau, đó là chìa khóa. Tránh điều đó và bạn sẽ có thể tránh được vấn đề.

+0

Cảm ơn bạn rất nhiều vì ý tưởng này! Bây giờ mã biên dịch trong phiên bản mới của Delphi một lần nữa. :-) Tôi cũng đã báo cáo lỗi: https://quality.embarcadero.com/browse/RSP-10575. Btw, bạn có nghĩ rằng việc sử dụng accessors và typeinfo là khả năng duy nhất để đọc tất cả dữ liệu cảm biến có sẵn không? Theo tôi, nó có vẻ là một phong cách lập trình ô uế. –

+0

Tôi không quen với FMX, đặc biệt không phải là trình biên dịch di động. Nhưng RTTI cho điều này thực sự cảm thấy khá sai. Người ta cảm thấy rằng phải có một cách tốt hơn ....... –

Các vấn đề liên quan