Beispiel #1
0
        public PersonViewModel(IPropertyProviderFactory propertyProviderFactory, IMessageBus messageBus, IDialogService dialogService, Person model)
        {
            _model = model;
            var pp = propertyProviderFactory.Create<IPersonViewModel>(this);

            // creating properties
            _name = pp.CreateProperty(i => i.Name, model.Name);
            _surname = pp.CreateProperty(i => i.Surname, model.Surname);
            _age = pp.CreateProperty(i => i.Age, model.Age);
            _address1 = pp.CreateProperty(i => i.Address1, model.Address1);
            _address2 = pp.CreateProperty(i => i.Address2, model.Address2);
            _address3 = pp.CreateProperty(i => i.Address3, model.Address3);
            _city = pp.CreateProperty(i => i.City, model.City);

            // creating commands
            _editCommand = pp.CreateCommand<Unit>(i => i.EditCommand);
            _saveCommand = pp.CreateCommand<Unit>(i => i.SaveCommand, !string.IsNullOrEmpty(model.Name) && !string.IsNullOrEmpty(model.Surname));
            _deleteCommand = pp.CreateCommand<Unit>(i => i.DeleteCommand);
            _closeCommand = pp.CreateCommand<Unit>(i => i.CloseCommand);

            // Observing commands
            ShouldDispose(_name.CombineLatest(_surname, (n,s) => !string.IsNullOrEmpty(n) && !string.IsNullOrEmpty(s)).Subscribe(_saveCommand.SetCanExecute));
            ShouldDispose(_saveCommand.OnExecute.Subscribe(_ =>
            {
                UpdateModel();
                _closeSubject.OnNext(true);
            }));
            ShouldDispose(_editCommand.OnExecute.Subscribe(_ => dialogService.ShowViewModel("Edit Person", this)));
            ShouldDispose(_deleteCommand.OnExecute.Subscribe(_ => messageBus.Publish(new DeletePersonMessage(this))));
            ShouldDispose(_closeCommand.OnExecute.Subscribe(_ =>
            {
                ResetData();
                _closeSubject.OnNext(false);
            }));
        }
Beispiel #2
0
 private void UpdateModel()
 {
     _model = this.ToModel();
 }
 public IPersonViewModel Create(Person person)
 {
     return new PersonViewModel(_propertyProviderFactory, _messageBus, _dialogService, person);
 }