Пример #1
0
        private void RegisterCallerForStateTracking(string caller)
        {
            var instanceType = TypeDescriptor.GetReflectionType(this);
            var propertyInfo = caller != null?instanceType.GetProperty(caller) : null;

            var dateTime   = DateTime.Now;
            var infoString = $"{propertyInfo?.ReflectedType.Name}.{propertyInfo?.Name}";
            var key        = $"{dateTime.ToLongTimeString()}.{dateTime.Millisecond} {(infoString.Length > 1 ? infoString : $"{dateTime.ToLongTimeString()}.{dateTime.Millisecond} {GetType().ToString()}")}";

            if (propertyInfo is null)
            {
                // in case of Collection.Add the constructor of added class should fire NotifyPropertyChanged
                _mementoService.RegisterObject(this);
                _mementoHashtable.Add(key, this);
                return;
            }

            var propertyValueType = propertyInfo.GetValue(this);

            if (propertyValueType is INotifyCollectionChanged notifyCollectionChanged)
            {
                // double registration will be checked by MementoService it self
                _mementoService.RegisterCollection(notifyCollectionChanged);
                key += COLLECTION_INDICATOR;
            }
            else if (propertyValueType is INotifyPropertyChanged notifyPropertyChanged)
            {
                // double registration will be checked by MementoService it self
                _mementoService.RegisterObject(notifyPropertyChanged);
                key += OBJECT_INDICATOR;
            }

            _mementoHashtable.Add(key, propertyValueType);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
        /// </summary>
        public MainWindowViewModel(IUIVisualizerService uiVisualizerService, IMessageService messageService, IMementoService mementoService)
        {
            _uiVisualizerService = uiVisualizerService;
            _messageService = messageService;
            _mementoService = mementoService;

            Add = new TaskCommand(OnAddExecuteAsync);
            Edit = new TaskCommand(OnEditExecuteAsync, OnEditCanExecute);
            Remove = new TaskCommand(OnRemoveExecuteAsync, OnRemoveCanExecute);

            Undo = new Command(() => _mementoService.Undo(), () => _mementoService.CanUndo);
            Redo = new Command(() => _mementoService.Redo(), () => _mementoService.CanRedo);

            PersonCollection = new ObservableCollection<Person> {new Person {Gender = Gender.Male, FirstName = "Geert", MiddleName = "van", LastName = "Horrik"}, new Person {Gender = Gender.Male, FirstName = "Fred", MiddleName = "", LastName = "Retteket"}};

            _mementoService.RegisterCollection(PersonCollection);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
        /// </summary>
        public MainWindowViewModel(IUIVisualizerService uiVisualizerService, IMessageService messageService, IMementoService mementoService)
        {
            _uiVisualizerService = uiVisualizerService;
            _messageService      = messageService;
            _mementoService      = mementoService;

            Add    = new Command(OnAddExecute);
            Edit   = new Command(OnEditExecute, OnEditCanExecute);
            Remove = new Command(OnRemoveExecute, OnRemoveCanExecute);

            Undo = new Command(() => _mementoService.Undo(), () => _mementoService.CanUndo);
            Redo = new Command(() => _mementoService.Redo(), () => _mementoService.CanRedo);

            PersonCollection = new ObservableCollection <Person> {
                new Person {
                    Gender = Gender.Male, FirstName = "Geert", MiddleName = "van", LastName = "Horrik"
                }, new Person {
                    Gender = Gender.Male, FirstName = "Fred", MiddleName = "", LastName = "Retteket"
                }
            };

            _mementoService.RegisterCollection(PersonCollection);
        }