/// <summary>
        /// Creates a <see cref="DataSourceDefinition"/> structure for the specified XML document.
        /// </summary>
        /// <param name="xdocument">The XML document from which to load a data source definition.</param>
        /// <param name="namespace">The namespace within which to place the compiled view model.</param>
        /// <param name="name">The name of the data source's view model.</param>
        /// <param name="path">The path to the file that defines the data source.</param>
        /// <returns>The instance of <see cref="DataSourceDefinition"/> that was created, or <see langword="null"/> if the specified
        /// XML does not contain a valid view.</returns>
        public static DataSourceDefinition? CreateDataSourceDefinitionFromXml(XDocument xdocument, String @namespace, String name, String path)
        {
            if (xdocument.Root.Name.LocalName != "UIPanelDefinition")
                return null;

            var viewdef = xdocument.Root.Element("View");
            if (viewdef == null)
                return null;

            return DataSourceDefinition.FromView(@namespace, name, path, viewdef);
        }
Пример #2
0
        /// <summary>
        /// Creates a new instance of <see cref="DataSourceWrapperInfo"/> that represents the specified data source wrapper.
        /// </summary>
        /// <param name="state">The expression compiler's current state.</param>
        /// <param name="dataSourceDefinition">The data source definition for which to retrieve data source wrapper info.</param>
        /// <returns>The <see cref="DataSourceWrapperInfo"/> that was created to represent the specified data source.</returns>
        public static DataSourceWrapperInfo GetDataSourceWrapperInfo(IExpressionCompilerState state, DataSourceDefinition dataSourceDefinition)
        {
            var dataSourceWrappedType = dataSourceDefinition.TemplatedControl;

            if (dataSourceWrappedType == null)
            {
                var definedDataSourceTypeAttr = dataSourceDefinition.Definition.Attribute("ViewModelType");
                var definedDataSourceTypeName = (String)definedDataSourceTypeAttr;
                if (definedDataSourceTypeName == null)
                {
                    return(null);
                }

                var typeNameCommaIx = definedDataSourceTypeName.IndexOf(',');
                if (typeNameCommaIx < 0)
                {
                    throw new BindingExpressionCompilationErrorException(definedDataSourceTypeAttr, dataSourceDefinition.DefinitionPath,
                                                                         CompilerStrings.ViewModelTypeIsNotFullyQualified.Format(definedDataSourceTypeName));
                }

                var definedDataSourceType = Type.GetType(definedDataSourceTypeName);
                if (definedDataSourceType == null)
                {
                    throw new BindingExpressionCompilationErrorException(definedDataSourceTypeAttr, dataSourceDefinition.DefinitionPath,
                                                                         PresentationStrings.ViewModelTypeNotFound.Format(definedDataSourceTypeName));
                }

                dataSourceWrappedType = definedDataSourceType;
            }

            var dataSourceWrapperName        = dataSourceDefinition.DataSourceWrapperName;
            var dataSourceWrapperExpressions = new List <BindingExpressionInfo>();

            foreach (var element in dataSourceDefinition.Definition.Elements())
            {
                FindBindingExpressionsInDataSource(state,
                                                   dataSourceDefinition, dataSourceWrappedType, element, dataSourceWrapperExpressions);
            }

            dataSourceWrapperExpressions = CollapseDataSourceExpressions(dataSourceWrapperExpressions);

            var dataSourceReferences = new List <String>();
            var dataSourceImports    = new List <String>();

            var xmlRoot       = dataSourceDefinition.Definition.Parent;
            var xmlDirectives = xmlRoot.Elements("Directive");

            foreach (var xmlDirective in xmlDirectives)
            {
                var xmlDirectiveType = (String)xmlDirective.Attribute("Type");
                if (String.IsNullOrEmpty(xmlDirectiveType))
                {
                    throw new BindingExpressionCompilationErrorException(xmlDirective, dataSourceDefinition.DefinitionPath,
                                                                         CompilerStrings.ViewDirectiveMustHaveType);
                }

                var xmlDirectiveTypeName = xmlDirectiveType.ToLowerInvariant();
                var xmlDirectiveValue    = xmlDirective.Value.Trim();
                switch (xmlDirectiveTypeName)
                {
                case "import":
                {
                    if (String.IsNullOrEmpty(xmlDirectiveValue))
                    {
                        throw new BindingExpressionCompilationErrorException(xmlDirective, dataSourceDefinition.DefinitionPath,
                                                                             CompilerStrings.ViewDirectiveHasInvalidValue);
                    }
                    dataSourceImports.Add(xmlDirective.Value.Trim());
                }
                break;

                case "reference":
                {
                    if (String.IsNullOrEmpty(xmlDirectiveValue))
                    {
                        throw new BindingExpressionCompilationErrorException(xmlDirective, dataSourceDefinition.DefinitionPath,
                                                                             CompilerStrings.ViewDirectiveHasInvalidValue);
                    }
                    dataSourceReferences.Add(xmlDirective.Value.Trim());
                }
                break;

                default:
                    throw new BindingExpressionCompilationErrorException(xmlDirective, dataSourceDefinition.DefinitionPath,
                                                                         CompilerStrings.ViewDirectiveNotRecognized.Format(xmlDirectiveTypeName));
                }
            }

            var frameworkTemplates = new Dictionary <String, XElement>();

            FindFrameworkTemplateElements(dataSourceDefinition.Definition, frameworkTemplates);

            var frameworkTemplateWrapperDefs = frameworkTemplates.Select(x =>
                                                                         DataSourceDefinition.FromView(dataSourceDefinition.DataSourceWrapperNamespace, x.Key, dataSourceDefinition.DefinitionPath, x.Value)).ToList();

            var frameworkTemplateWrapperInfos = frameworkTemplateWrapperDefs.Select(definition =>
                                                                                    GetDataSourceWrapperInfoForFrameworkTemplate(state, dataSourceReferences, dataSourceImports, definition)).ToList();

            return(new DataSourceWrapperInfo()
            {
                References = dataSourceReferences,
                Imports = dataSourceImports,
                DataSourceDefinition = dataSourceDefinition,
                DataSourcePath = dataSourceDefinition.DefinitionPath,
                DataSourceType = dataSourceWrappedType,
                DataSourceWrapperName = dataSourceWrapperName,
                Expressions = dataSourceWrapperExpressions,
                DependentWrapperInfos = frameworkTemplateWrapperInfos
            });
        }