Exemplo n.º 1
0
        /// <summary>
        /// Tries to find a match between a property of the given type
        /// and the given view model.
        /// </summary>
        /// <param name="viewModel">The view model of a view being looked for.</param>
        /// <param name="viewType">The type of an object which properties are analyzed.</param>
        /// <param name="viewFactory">The view factory called when a match happens.</param>
        /// <returns>The result of search of an object implementing a view for the given view model;
        /// otherwise null.</returns>
        public static ViewModelPropertyMatch TryMatch(object viewModel, Type viewType, Func <object> viewFactory)
        {
            Contract.Requires <ArgumentNullException>(viewModel != null);
            Contract.Requires <ArgumentNullException>(viewType != null);
            Contract.Requires <ArgumentNullException>(viewFactory != null);

            var viewModelProperty = viewType
                                    .GetProperties()
                                    .Where(pi => pi.IsDefined(typeof(ViewModelAttribute), false))
                                    .FirstOrDefault(pi => ViewModelPropertyMatch.IsPropertyMatchToModel(pi, viewModel));

            if (viewModelProperty == null)
            {
                return(null);
            }

            // Создаем представление и задаем ему модель.
            var match = new ViewModelPropertyMatch(viewFactory(), viewModelProperty);

            match.ChangeModel(viewModel);

            return(match);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Gets whether the object implements a view for the given model.
 /// </summary>
 /// <param name="viewModel">The view model to be checked.</param>
 /// <returns>true, if the given object implements a view for the given model;
 /// otherwise false.</returns>
 public bool IsMatchToModel(object viewModel)
 {
     return(ViewModelPropertyMatch.IsPropertyMatchToModel(this._viewModelProperty, viewModel));
 }