delphimvvmlivebindings

How create binded object outside of PrototypeBindSource.OnCreateAdapter?


I'm using TPrototypeBindSource to bind some object property to visual control. Everything works correctly but I have to create this object in TPrototypeBindSource.OnCreateAdapter like that:

procedure TForm.PrototypeBindSourceCreateAdapter(Sender: TObject;
  var ABindSourceAdapter: TBindSourceAdapter);
begin
    _viewModel := TViewModel.Create;
    ABindSourceAdapter := TObjectBindSourceAdapter<TViewModel>.Create(self,
      _viewModel);
end;

I want move creating _viewModel to form's constructor but then it stops working. Probably because OnCreateAdapter is calling before FormCreate. There is any way to create _viewModel outside of OnCreateAdapter event?

edited: Delphi Tokyo 10.2


Solution

  • I have just found nice tutorial where this problem is resolved. https://delphiaball.co.uk/2015/10/19/livebindings-in-vcl-part-2-livebinding-objects/ You have to override form constructor and create _viewModel before inherited Create is called.

    constructor TForm.Create;
    begin
        _viewModel := TViewModel;
        inherited Create(Application);
    end;