我使用TObjectBindSourceAdapter
使用livebindings與對象。 與TObjectBindSourceAdapter
使用的對象的屬性之一具有枚舉類型,但在我的對象中使用枚舉類型時從不會生成適配器中的字段如何(正確)使用枚舉類型livebindings(TObjectBindSourceAdapter)
我現在找到的唯一解決方案是在我的對象中定義枚舉類型爲整數,並對它進行類型轉換。這似乎工作正常,但你必須保持類型轉換和返回枚舉類型和整數。
下面是一些示例代碼來解釋我的意思。
它採用枚舉類型,我最初嘗試似乎並沒有工作第一個例子:
uses Data.Bind.ObjectScope;
Type
TMyEnumtype = (meOne, meTwo, meThree);
TMyObject = class
public
MyEnumType: TMyEnumtype;
end;
procedure TForm9.But1Click(Sender: TObject);
var
MyObject: TMyObject;
aBindSourceAdapter: TBindSourceAdapter;
begin
MyObject := TMyObject.Create;
MyObject.MyEnumType := meTwo;
aBindSourceAdapter := TObjectBindSourceAdapter<TMyObject>.Create(nil, MyObject, False);
if aBindSourceAdapter.FindField('MyEnumType') <> nil then
ShowMessage('MyEnumType found')
else
showmessage('MyEnumType not found');
FreeAndNil(MyObject);
FreeAndNil(aBindSourceAdapter);
end;
,似乎通過類型轉換爲整數
uses Data.Bind.ObjectScope;
Type
TMyEnumtype = (meOne, meTwo, meThree);
TMyObject = class
public
MyEnumType: integer;
end;
procedure TForm9.But1Click(Sender: TObject);
var
MyObject: TMyObject;
aBindSourceAdapter: TBindSourceAdapter;
aEnumType : TMyEnumtype;
begin
MyObject := TMyObject.Create;
MyObject.MyEnumType := Integer(meTwo);
aBindSourceAdapter := TObjectBindSourceAdapter<TMyObject>.Create(nil, MyObject, False);
if aBindSourceAdapter.FindField('MyEnumType') <> nil then
ShowMessage('MyEnumType found')
else
showmessage('MyEnumType not found');
aEnumType := TMyEnumtype(aBindSourceAdapter.FindField('MyEnumType').GetTValue.AsInteger);
if aEnumType = meTwo then
showmessage('meTwo');
FreeAndNil(MyObject);
FreeAndNil(aBindSourceAdapter);
end;
我在想,如果工作第二個例子別人都遇到過這個問題,如果有可能是一些其他的解決方案來解決這個沒有恢復到整數,同時繼續使用枚舉類型。我也不確定我的解決方法是否是通用的方法。
我認爲你別無選擇 - 我很早以前就得出結論:鑄造爲整數是最好的解決方案。 –