Esempio n. 1
0
        /// <summary>
        /// Given the type of a model, locate the type of its View (or throw an exception)
        /// </summary>
        /// <param name="modelType">Model to find the view for</param>
        /// <returns>Type of the ViewModel's View</returns>
        protected virtual Type LocateViewForModel(Type modelType)
        {
            var modelName = modelType.FullName;
            var viewName = ViewTypeNameForModelTypeName(modelName);

            if (modelName == viewName)
            {
                throw new StyletViewLocationException(string.Format("Unable to transform ViewModel name {0} into a suitable View name", modelName), viewName);
            }

            var viewType = ViewTypeForViewName(viewName);

            if (viewType == null)
            {
                var e = new StyletViewLocationException(string.Format("Unable to find a View with type {0}", viewName), viewName);
                logger.Error(e);
                throw e;
            }
            else
            {
                logger.Info("Searching for a View with name {0}, and found {1}", viewName, viewType);
            }

            return viewType;
        }
Esempio n. 2
0
        /// <summary>
        /// Given a ViewModel instance, locate its View type (using LocateViewForModel), and instantiates it
        /// </summary>
        /// <param name="model">ViewModel to locate and instantiate the View for</param>
        /// <returns>Instantiated and setup view</returns>
        public virtual UIElement CreateViewForModel(object model)
        {
            var viewType = LocateViewForModel(model.GetType());

            if (viewType.IsAbstract || !typeof(UIElement).IsAssignableFrom(viewType))
            {
                var e = new StyletViewLocationException(string.Format("Found type for view: {0}, but it wasn't a class derived from UIElement", viewType.Name), viewType.Name);
                logger.Error(e);
                throw e;
            }

            var view = (UIElement)ViewFactory(viewType);

            InitializeView(view, viewType);

            return view;
        }