/// <inheritdoc /> public IViewModel CreateViewModel(Type viewModelType, IViewModel parentVM, ViewModelOptions viewModelOptions) { lock (_viewModels) { IViewModel vm = (IViewModel)_container.Resolve(viewModelType); if (viewModelOptions == null) { viewModelOptions = vm.DefaultViewModelOptions; } vm.SetViewModelOptions(viewModelOptions); var id = ++_lastID; _logger?.Log(_eventsSource, $"ViewModelCreated {viewModelType.Name}", LogRecordTypes.Debug); var vmInfo = new ViewModelInfo(vm) { ID = id }; if (!_viewModels.Any() && parentVM == null) { RootViewModel = vmInfo; } else if (parentVM == null && RootViewModel != null) { parentVM = RootViewModel.ViewModel; } if (parentVM != null && _viewModels.ContainsKey(parentVM)) { var parent = _viewModels[parentVM]; vmInfo.ParentViewModel = parent; parent.ChildViewModels.Add(vmInfo); } _viewModels.TryAdd(vm, vmInfo); ViewModelCreated?.Invoke(this, vmInfo); return(vm); } }
/// <summary> /// Creates a child ViewModel /// </summary> protected TResult CreateViewModel <TResult>(ViewModelOptions viewModelOptions) where TResult : IViewModel { return(_vmManager.CreateViewModel <TResult>(this, viewModelOptions)); }
/// <summary> /// Creates a child ViewModel /// </summary> protected IViewModel CreateViewModel(Type viewModelType, ViewModelOptions viewModelOptions) { return(_vmManager.CreateViewModel(viewModelType, this, viewModelOptions)); }
/// <summary> /// Set the <see cref="ViewModelOptions"/> property /// </summary> /// <param name="options"></param> public virtual void SetViewModelOptions(ViewModelOptions options) { ViewModelOptions = options; }
/// <inheritdoc /> public TResult CreateViewModel <TResult>(IViewModel parentVM, ViewModelOptions viewModelOptions) where TResult : IViewModel { return((TResult)CreateViewModel(typeof(TResult), parentVM, viewModelOptions)); }