Catel.MVVM.ManagedViewModel.AddViewModelInstance C# (CSharp) Method

AddViewModelInstance() public method

Adds a view model instance to the list of instances.
The is null. The is not of the right type.
public AddViewModelInstance ( IViewModel viewModel ) : void
viewModel IViewModel The view model instance to add.
return void
        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);
                }
            }
        }

Usage Example

Esempio n. 1
0
        public void AddViewModelInstance_NewInstance()
        {
            ViewModelManager.ClearAll();

            var viewModel = new ManagedViewModel(typeof (InterestingViewModel));
            viewModel.AddViewModelInstance(new InterestingViewModel());
        }
All Usage Examples Of Catel.MVVM.ManagedViewModel::AddViewModelInstance