Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FrameworkTemplate"/> class.
        /// </summary>
        /// <param name="template">The UVML template that this instance represents.</param>
        /// <param name="dataSourceWrapperName">The name of the template's data source wrapper type.</param>
        protected FrameworkTemplate(UvmlTemplate template, String dataSourceWrapperName)
        {
            Contract.Require(template, nameof(template));

            this.template = template;
            this.dataSourceWrapperName = dataSourceWrapperName;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new <see cref="UvmlTemplate"/> from the specified XML element.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="xml">The XML element to load as a UVML template.</param>
        /// <param name="templatedParentType">The type of the object's templated parent.</param>
        /// <param name="templatedObjectType">The type of object which is produced by the template.</param>
        /// <param name="cultureInfo">The <see cref="CultureInfo"/> to use when parsing values.</param>
        public static UvmlTemplate CreateTemplateFromXml(UltravioletContext uv,
            XElement xml, Type templatedParentType, Type templatedObjectType, CultureInfo cultureInfo)
        {
            var mutators = new List<UvmlMutator>();

            templatedObjectType = ResolveTypeFromName(uv, templatedParentType, templatedObjectType, xml.Name.LocalName);

            // Create mutators from attributes
            var attrs = xml.Attributes().ToList();
            foreach (var attr in attrs)
            {
                switch (attr.Name.LocalName)
                {
                    case "Name":
                    case "Class":
                    case "ViewModelType":
                    case "BindingContext":
                        continue;
                }

                var mutator = CreateMutatorForXmlAttribute(uv, xml, attr, templatedParentType, templatedObjectType, cultureInfo);
                mutators.Add(mutator);
            }

            // Sort elements into "properties/events" and "children"
            var elems = xml.Elements().ToList();
            var elemsRepresentingProperties = elems.Where(x => x.Name.LocalName.Contains('.')).ToList();
            var elemsRepresentingChildren = elems.Except(elemsRepresentingProperties).ToList();

            // Create mutators from elements representing properties
            if (elemsRepresentingProperties.Any())
            {
                foreach (var elem in elemsRepresentingProperties)
                {
                    var mutator = CreateMutatorForXmlElement(uv, xml, elem, templatedParentType, templatedObjectType, cultureInfo);
                    mutators.Add(mutator);
                }
            }

            // Create mutators from child elements
            if (elemsRepresentingChildren.Any())
            {
                var mutator = CreateMutatorForXmlChildren(uv, xml, elemsRepresentingChildren, templatedParentType, templatedObjectType, cultureInfo);
                mutators.Add(mutator);
            }
            else
            {
                // Create mutator for simple default property
                var mutator = CreateMutatorForLiteralDefaultProperty(uv, xml, templatedParentType, templatedObjectType, cultureInfo);
                if (mutator != null)
                {
                    mutators.Add(mutator);
                }
            }

            var template = new UvmlTemplate(xml, templatedObjectType, null, mutators);
            template.IsItemsPanelForTemplatedParent = typeof(UIElement).IsAssignableFrom(templatedObjectType) &&
                String.Equals(xml.Name.LocalName, "ItemsPanel", StringComparison.Ordinal);

            return template;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads an instance of the <see cref="PresentationFoundationView"/> from an XML node.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="uiPanel">The <see cref="UIPanel"/> that is creating the panel.</param>
        /// <param name="uiPanelDefinition">The <see cref="UIPanelDefinition"/> that defines the view.</param>
        /// <param name="vmfactory">A view model factory which is used to create the view's initial view model, or <see langword="null"/> to skip view model creation.</param>
        /// <returns>The <see cref="PresentationFoundationView"/> that was loaded from the specified XML element.</returns>
        public static PresentationFoundationView Load(UltravioletContext uv, UIPanel uiPanel, UIPanelDefinition uiPanelDefinition, UIViewModelFactory vmfactory)
        {
            Contract.Require(uv, nameof(uv));
            Contract.Require(uiPanel, nameof(uiPanel));
            Contract.Require(uiPanelDefinition, nameof(uiPanelDefinition));

            var viewElement = uiPanelDefinition.ViewElement;
            if (viewElement == null)
                return null;

            // Determine which culture to use when parsing values.
            var cultureRequested = (String)uiPanelDefinition.RootElement.Attribute("Culture");
            var cultureInfo = CultureInfo.GetCultureInfo(cultureRequested ?? String.Empty);

            // Determine the type of view model used by this view.
            var viewModelType = default(Type);
            var viewModelTypeName = (String)viewElement.Attribute("ViewModelType");
            if (viewModelTypeName != null)
            {
                viewModelType = Type.GetType(viewModelTypeName, false);

                if (viewModelType == null)
                    throw new UvmlException(PresentationStrings.ViewModelTypeNotFound.Format(viewModelTypeName));

                var viewModelWrapperAttr = viewModelType.GetCustomAttributes(typeof(ViewModelWrapperAttribute), false)
                    .Cast<ViewModelWrapperAttribute>().SingleOrDefault();
                if (viewModelWrapperAttr != null)
                {
                    viewModelType = viewModelWrapperAttr.WrapperType;
                }
                else
                {
                    var viewModelWrapperName = PresentationFoundationView.GetDataSourceWrapperNameForView(uiPanelDefinition.AssetFilePath);
                    var viewModelWrapperType = uv.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(viewModelWrapperName);
                    if (viewModelWrapperType == null)
                        throw new UvmlException(PresentationStrings.CannotFindViewModelWrapper.Format(viewModelWrapperName));

                    viewModelType = viewModelWrapperType;
                }
            }

            // Create a UVML template which will instantiate the view.
            AddUvmlAnnotations(viewModelType.Name, viewElement);
            var viewTemplate = new UvmlTemplate(viewElement, typeof(PresentationFoundationView), (puv, pname) =>
            {
                var view = new PresentationFoundationView(puv, uiPanel, viewModelType);
                var viewModel = vmfactory == null ? null : vmfactory(view);
                if (viewModel != null)
                {
                    view.SetViewModel(viewModel);
                }

                var root = view.LayoutRoot;
                root.BeginInit();

                var rootAdornerDecorator = new AdornerDecorator(puv, null);
                rootAdornerDecorator.BeginInit();
                root.Child = rootAdornerDecorator;

                var rootGridTemplate = CreateTemplateFromXml(puv, viewElement, null, typeof(Grid), cultureInfo);
                var rootGridContext = UvmlInstantiationContext.ForView(puv, view);

                var rootGridTemplateInstance = (UvmlTemplateInstance)rootGridTemplate.Instantiate(puv, rootGridContext);
                var rootGrid = (Grid)rootGridTemplateInstance.Finalize();

                rootAdornerDecorator.Child = rootGrid;

                return view;
            });

            // Instantiate the view template.
            var viewTemplateInstance = (UvmlTemplateInstance)viewTemplate.Instantiate(uv, null);
            var viewInstance = (PresentationFoundationView)viewTemplateInstance.Finalize();

            var viewRoot = viewInstance.LayoutRoot;
            var viewRootAdornerDecorator = (AdornerDecorator)viewRoot.Child;
            viewRootAdornerDecorator.EndInit();
            viewRoot.EndInit();
            viewRoot.CacheLayoutParameters();

            var viewInstanceViewModel = viewInstance.ViewModel;
            if (viewInstanceViewModel != null)
                viewInstance.Namescope.PopulateFieldsFromRegisteredElements(viewInstanceViewModel);

            return viewInstance;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataTemplate"/> class.
 /// </summary>
 /// <param name="template">The UVML template that this instance represents.</param>
 /// <param name="dataSourceWrapperName">The name of the template's data source wrapper type.</param>
 public DataTemplate(UvmlTemplate template, String dataSourceWrapperName)
     : base(template, dataSourceWrapperName)
 {
 }