Ejemplo n.º 1
0
        /// <summary>
        /// Adds a view model instance to the list of instances.
        /// </summary>
        /// <param name="viewModel">The view model instance to add.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="viewModel"/> is <c>null</c>.</exception>
        /// <exception cref="WrongViewModelTypeException">The <paramref name="viewModel"/> is not of the right type.</exception>
        public void AddViewModelInstance(IViewModel viewModel)
        {
            Argument.IsNotNull("viewModel", viewModel);

            if (viewModel.GetType() != ViewModelType)
            {
                throw Log.ErrorAndCreateException(msg => new WrongViewModelTypeException(viewModel.GetType(), ViewModelType),
                                                  "Cannot use view model type '{0}', expected type '{1}'", viewModel.GetType().GetSafeFullName(false), ViewModelType.GetSafeFullName(false));
            }

            lock (_lock)
            {
                var vmId = viewModel.UniqueIdentifier;
                if (!_viewModelInstances.ContainsKey(vmId))
                {
                    _viewModelInstances.Add(vmId, viewModel);

                    viewModel.PropertyChanged += OnViewModelPropertyChanged;
                    var viewModelBase = viewModel as ViewModelBase;
                    if (viewModelBase != null)
                    {
                        viewModelBase.CommandExecutedAsync += OnViewModelCommandExecutedAsync;
                    }

                    viewModel.SavingAsync    += OnViewModelSavingAsync;
                    viewModel.SavedAsync     += OnViewModelSavedAsync;
                    viewModel.CancelingAsync += OnViewModelCancelingAsync;
                    viewModel.CanceledAsync  += OnViewModelCanceledAsync;
                    viewModel.ClosedAsync    += OnViewModelClosedAsync;

                    Log.Debug("Added view model instance, currently containing '{0}' instances of type '{1}'", _viewModelInstances.Count, ViewModelType);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a view model instance to the list of instances.
        /// </summary>
        /// <param name="viewModel">The view model instance to add.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="viewModel"/> is <c>null</c>.</exception>
        /// <exception cref="WrongViewModelTypeException">The <paramref name="viewModel"/> is not of the right type.</exception>
        public void AddViewModelInstance(IViewModel viewModel)
        {
            Argument.IsNotNull("viewModel", viewModel);

            if (viewModel.GetType() != ViewModelType)
            {
                throw Log.ErrorAndCreateException(msg => new WrongViewModelTypeException(viewModel.GetType(), ViewModelType),
                                                  "Cannot use view model type '{0}', expected type '{1}'", viewModel.GetType().GetSafeFullName(false), ViewModelType.GetSafeFullName(false));
            }

            lock (_lock)
            {
                var vmId = viewModel.UniqueIdentifier;
                if (!_viewModelInstances.ContainsKey(vmId))
                {
                    _viewModelInstances.Add(vmId, viewModel);

                    Log.Debug("Added view model instance, currently containing '{0}' instances of type '{1}'", _viewModelInstances.Count, ViewModelType);
                }
            }
        }