Esempio n. 1
0
        /// <summary>
        /// Tries to construct the view model using the argument. If that fails, it will try to use
        /// the default constructor of the view model. If that is not available, <c>null</c> is returned.
        /// </summary>
        /// <param name="injectionObject">The object that is injected into the view model constructor.</param>
        /// <param name="viewModelType">Type of the view model.</param>
        /// <returns>Constructed view model or <c>null</c> if the view model could not be constructed.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="viewModelType"/> is <c>null</c>.</exception>
        private IViewModel ConstructViewModelUsingArgumentOrDefaultConstructor(object injectionObject, Type viewModelType)
        {
            Argument.IsNotNull("viewModelType", viewModelType);

            if (ViewModelBehavior == LogicViewModelBehavior.Injected)
            {
                return(ViewModel);
            }

            if (PreventViewModelCreation)
            {
                Log.Info("ViewModel construction is prevented by the PreventViewModelCreation property");
                return(null);
            }

            if (IgnoreNullDataContext && (injectionObject == null))
            {
                Log.Info("ViewModel construction is prevented by the IgnoreNullDataContext property");
                return(null);
            }

            var determineViewModelInstanceHandler = DetermineViewModelInstance;

            if (determineViewModelInstanceHandler != null)
            {
                var determineViewModelInstanceEventArgs = new DetermineViewModelInstanceEventArgs(injectionObject);
                determineViewModelInstanceHandler(this, determineViewModelInstanceEventArgs);
                if (determineViewModelInstanceEventArgs.ViewModel != null)
                {
                    var viewModel = determineViewModelInstanceEventArgs.ViewModel;
                    Log.Info("ViewModel instance is overriden by the DetermineViewModelInstance event, using view model of type '{0}'", viewModel.GetType().Name);

                    return(viewModel);
                }

                if (determineViewModelInstanceEventArgs.DoNotCreateViewModel)
                {
                    Log.Info("ViewModel construction is prevented by the DetermineViewModelInstance event (DoNotCreateViewModel is set to true)");
                    return(null);
                }
            }

            var determineViewModelTypeHandler = DetermineViewModelType;

            if (determineViewModelTypeHandler != null)
            {
                var determineViewModelTypeEventArgs = new DetermineViewModelTypeEventArgs(injectionObject);
                determineViewModelTypeHandler(this, determineViewModelTypeEventArgs);
                if (determineViewModelTypeEventArgs.ViewModelType != null)
                {
                    Log.Info("ViewModelType is overriden by the DetermineViewModelType event, using '{0}' instead of '{1}'",
                             determineViewModelTypeEventArgs.ViewModelType.FullName, viewModelType.FullName);

                    viewModelType = determineViewModelTypeEventArgs.ViewModelType;
                }
            }

            var injectionObjectAsViewModel = injectionObject as IViewModel;

            if (injectionObjectAsViewModel != null)
            {
                var injectionObjectViewModelType = injectionObject.GetType();

                if (ViewModelFactory.CanReuseViewModel(TargetViewType, viewModelType, injectionObjectViewModelType, injectionObject as IViewModel))
                {
                    Log.Info("DataContext of type '{0}' is allowed to be reused by view '{1}', using the current DataContext as view model",
                             viewModelType.FullName, TargetViewType.FullName);

                    return((IViewModel)injectionObject);
                }
            }

            Log.Debug("Using IViewModelFactory '{0}' to instantiate the view model", ViewModelFactory.GetType().FullName);

            var viewModelInstance = ViewModelFactory.CreateViewModel(viewModelType, injectionObject);

            Log.Debug("Used IViewModelFactory to instantiate view model, the factory did{0} return a valid view model",
                      (viewModelInstance != null) ? string.Empty : " NOT");

            return(viewModelInstance);
        }