/// <summary>
        /// Called when the object is validated.
        /// </summary>
        protected override void OnValidated()
        {
            bool updatedValidationSummaries = false;

            foreach (var validationSummaryInfo in _validationSummaries)
            {
                var isSummaryUpdateRequired = false;
                var lastUpdated             = _validationSummariesUpdateStamps.ContainsKey(validationSummaryInfo.Key) ? _validationSummariesUpdateStamps[validationSummaryInfo.Key] : 0L;

                isSummaryUpdateRequired = this.IsValidationSummaryOutdated(lastUpdated, validationSummaryInfo.Value.IncludeChildViewModels);
                if (!isSummaryUpdateRequired)
                {
                    continue;
                }

                IValidationSummary validationSummary;
                if (validationSummaryInfo.Value.UseTagToFilter)
                {
                    validationSummary = this.GetValidationSummary(validationSummaryInfo.Value.IncludeChildViewModels, validationSummaryInfo.Value.Tag);
                }
                else
                {
                    validationSummary = this.GetValidationSummary(validationSummaryInfo.Value.IncludeChildViewModels);
                }

                PropertyHelper.SetPropertyValue(this, validationSummaryInfo.Key, validationSummary);
                _validationSummariesUpdateStamps[validationSummaryInfo.Key] = validationSummary.LastModifiedTicks;

                updatedValidationSummaries = true;
            }

            if (updatedValidationSummaries)
            {
                ViewModelCommandManager.InvalidateCommands();
            }

            base.OnValidated();
        }
Example #2
0
        /// <summary>
        /// Registers the commands in a specific <see cref="IViewModel" /> instance. By subscribing
        /// to all commands, the <see cref="IViewModel.CommandExecutedAsync" /> can be intercepted.
        /// <para />
        /// This method will automatically subscribe to the <see cref="IViewModel.ClosedAsync"/> event and unsubscribe all commands
        /// at that time.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="viewModel"/> is <c>null</c>.</exception>
        public static IViewModelCommandManager Create(IViewModel viewModel)
        {
            Argument.IsNotNull("viewModel", viewModel);

            lock (_instances)
            {
                // Event the check for closed is done inside the lock. It might be that the lock has awaited the removal because the vm was being closed
                // in the meantime
                if (viewModel.IsClosed)
                {
                    Log.Warning("View model '{0}' with unique identifier '{1}' is already closed, cannot manage commands of a closed view model", viewModel.GetType().FullName, viewModel.UniqueIdentifier);
                    return(null);
                }

                if (!_instances.TryGetValue(viewModel.UniqueIdentifier, out var commandManager))
                {
                    commandManager = new ViewModelCommandManager(viewModel);
                    _instances[viewModel.UniqueIdentifier] = commandManager;
                }

                return(commandManager);
            }
        }