unit ViewForm.Main;
interface
uses
MVVM.Messenger.Messages, MVVM.ViewModel,
ViewModel.Main,
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs, ViewForm,
Vcl.ComCtrls,
Vcl.StdCtrls,
Vcl.ExtCtrls;
type
TMainFormView =
class( TFormView )
PageControl1 : TPageControl;
ListBox1 : TListBox;
Panel1 : TPanel;
AddView_Button : TButton;
GroupBox1 : TGroupBox;
Panel2 : TPanel;
GroupBox2 : TGroupBox;
Edit1 : TEdit;
ComboBox1 : TComboBox;
procedure AddView_ButtonClick( Sender : TObject );
private
FViewModel : TMainViewModel;
procedure AddPageView;
protected
procedure MsgPropertyChanged(
const AMessage : TPropertyChangedMessage );
override;
procedure DoLoadFromModel;
override;
procedure DoSaveToModel;
override;
procedure DoGetViewModel(
var ViewModel : TViewModel );
override;
public
procedure AfterConstruction;
override;
procedure BeforeDestruction;
override;
end;
var
MainFormView : TMainFormView;
implementation
{$R *.dfm}
{ TMainFormView }
procedure TMainFormView.AddPageView;
var
LViewClass : TPersistentClass;
LView : TFormView;
begin
// Die konkrete Klasse TDataEditFormView ist nicht bekannt (keine Abhängigkeiten=lose Koppelung)
// darum wird die über einen Alias gesucht
LViewClass := FindClass( '
DataEditView' );
if LViewClass.InheritsFrom( TFormView )
then
begin
LView := TFormClass( LViewClass ).Create( Self )
as TFormView;
// Verdrahtung der ViewModels
LView.ViewModel.Parent := FViewModel;
// ab ins PageControl
LView.ManualDock( PageControl1 );
LView.Align := alClient;
// und anzeigen
LView.Show;
end;
end;
procedure TMainFormView.AddView_ButtonClick( Sender : TObject );
begin
inherited;
AddPageView;
end;
procedure TMainFormView.AfterConstruction;
var
LView : TFormView;
begin
inherited;
FViewModel := TMainViewModel.Create;
// Eine View packen wir schon mal hinzu
AddPageView;
// Setze ich normalerweise im OI (Bequemlichkeit)
PageControl1.OnChange := ControlChange;
Edit1.OnChange := ControlChange;
ComboBox1.OnChange := ControlChange;
end;
procedure TMainFormView.BeforeDestruction;
begin
inherited;
FreeAndNil( FViewModel );
end;
procedure TMainFormView.DoGetViewModel(
var ViewModel : TViewModel );
begin
inherited;
ViewModel := FViewModel;
end;
procedure TMainFormView.DoLoadFromModel;
begin
inherited;
PageControl1.ActivePageIndex := FViewModel.CurrentPageIndex;
Edit1.Text := FViewModel.Data.Str;
ComboBox1.ItemIndex := FViewModel.Data.Int;
end;
procedure TMainFormView.DoSaveToModel;
begin
inherited;
FViewModel.CurrentPageIndex := PageControl1.ActivePageIndex;
FViewModel.Data.Str := Edit1.Text;
FViewModel.Data.Int := ComboBox1.ItemIndex;
end;
procedure TMainFormView.MsgPropertyChanged(
const AMessage : TPropertyChangedMessage );
begin
inherited;
ListBox1.ItemIndex := ListBox1.Items.Add( Format( '
%s(%x).%s', [AMessage.Sender.ClassName, Integer( AMessage.Sender ), AMessage.PropName] ) );
end;
end.