Exemple #1
0
        /// <summary>
        /// Locates the view for the specified model instance.
        /// </summary>
        /// <param name="model">the model instance.</param>
        /// <param name="context">The context (or null).</param>
        /// <returns>The view.</returns>
        public UIElement LocateForModel(object model, string context)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var view = TryGetViewFromViewAware(model, context);

            if (view != null)
            {
                Log.Info("Using cached view for {0}.", model);
                return(view);
            }

            var modelType = model.GetType();
            var viewType  = _typeResolver.GetViewType(modelType, context);

            if (viewType == null)
            {
                Log.Error("Cannot find view for {0}.", modelType);
                return(new TextBlock {
                    Text = string.Format("Cannot find view for {0}.", modelType)
                });
            }

            view = IoC.GetInstance(viewType) as UIElement;
            if (view == null)
            {
                view = (UIElement)Activator.CreateInstance(viewType);
            }

            return(view);
        }
        /// <summary>
        /// Navigate to the specified model type.
        /// </summary>
        /// <param name="viewModelType">The model type to navigate to.</param>
        /// <param name="parameter">The object parameter to pass to the target.</param>
        /// <returns>Whether or not navigation succeeded.</returns>
        public bool NavigateToViewModel(Type viewModelType, object parameter = null)
        {
            var viewType = _viewModelTypeResolver.GetViewType(viewModelType, null);

            if (viewType == null)
            {
                throw new InvalidOperationException(string.Format("No view was found for {0}.", viewModelType.FullName));
            }

            return(Navigate(viewType, parameter));
        }
        /// <summary>
        /// Locates the view for the specified model instance.
        /// </summary>
        /// <param name="model">the model instance.</param>
        /// <param name="context">The context (or null).</param>
        /// <returns>The view.</returns>
        public UIElement LocateForModel(object model, string context)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var view = TryGetViewFromViewAware(model, context);

            if (view != null)
            {
                Log.Info("Using cached view for {0}.", model);
                return(view);
            }

            var modelType = model.GetType();
            var viewType  = _typeResolver.GetViewType(modelType, context);

            if (viewType == null)
            {
                Log.Error("Cannot find view for {0}.", modelType);
                return(new TextBlock {
                    Text = string.Format("Cannot find view for {0}.", modelType)
                });
            }

            view = IoC.GetAllInstances(viewType).FirstOrDefault() as UIElement;
            if (view != null)
            {
                return(view);
            }

            var viewTypeInfo  = viewType.GetTypeInfo();
            var uiElementInfo = typeof(UIElement).GetTypeInfo();

            if (viewTypeInfo.IsInterface || viewTypeInfo.IsAbstract || !uiElementInfo.IsAssignableFrom(viewTypeInfo))
            {
                Log.Error("Cannot create {0}.", viewType);
                return(new TextBlock {
                    Text = string.Format("Cannot create {0}.", viewType)
                });
            }

            view = (UIElement)Activator.CreateInstance(viewType);
            return(view);
        }