访问 Delphi 中的只写 属性

Access to a write-only property in Delphi

我很难访问渐变中使用的颜色,例如 firemonkey 中的 TRectangle。渐变属性(color 和 color1)被定义为只写,但有必要读取它们的值。

访问这些属性的读取值的正确方法是什么?

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects;

type
  TForm1 = class(TForm)
    Rectangle: TRectangle;
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
var
  ColorGradient1: TAlphaColor;
begin

  // Error [dcc32 Error]: E2130 Cannot read a write-only property
  ColorGradient1 := Rectangle.Fill.Gradient.Color1;

end;

end.

使用TGradient.Points property. Per the TGradient documentation:

A TGradient instance is used for creating a gradient pattern. A gradient defaults to having two points, and the colors of these two points are exposed as Color and Color1. But it can also have more than two colors, which requires modifying the TGradientPoints through the Points property. The gradient can start and end at any given point, and can be either linear (by default) or radial.

Points是属性的集合TGradientPoint objects, and the TGradientPoint.Color属性是read/write。在内部,TGradient.ColorTGradient.Color1 属性分别简单地写入 Points[0].ColorPoints[1].Color 属性。