Example #1
0
        /// <summary>
        /// Processes the generators.
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="classType">Type of the class.</param>
        /// <param name="method">The initialize method.</param>
        /// <param name="generateFields">if set to <c>true</c> [generate fields].</param>
        /// <returns></returns>
        public CodeExpression ProcessGenerators(object source, CodeTypeDeclaration classType, CodeMemberMethod method, bool generateFields)
        {
            if (source == null)
            {
                return(null);
            }

            IGeneratorType generator;
            Type           elementType = source.GetType();

            bool customUserControl = false;

            if (elementType.BaseType == typeof(UserControl))
            {
                // we have to retype any custom user control to fake generator so we can generate code for it
                customUserControl = true;
                elementType       = typeof(CustomUserControlGeneratorType);
            }

            if (Generators.TryGetValue(elementType, out generator))
            {
                DependencyObject xamlSource = source as DependencyObject;

                if (xamlSource != null)
                {
                    CodeExpression parent = generator.Generate(xamlSource, classType, method, generateFields);
                    CodeComHelper.GenerateAttachedProperties(method, parent, xamlSource);

                    Type oldDataType = BindingGenerator.Instance.ActiveDataType;
                    Type newType     = xamlSource.GetValue(GeneratedBindings.DataTypeProperty) as Type;
                    if (newType != null)
                    {
                        BindingGenerator.Instance.ActiveDataType = newType;
                    }

                    FrameworkElement elem = source as FrameworkElement;
                    if (elem != null)
                    {
                        CodeComHelper.GenerateBindings(method, parent, elem, elem.Name);
                        CodeComHelper.GenerateResourceReferences(method, parent, elem);

                        if (!customUserControl && (elem.Resources.Count != 0 || elem.Resources.MergedDictionaries.Count != 0))
                        {
                            ResourceDictionaryGenerator resourcesGenerator = new ResourceDictionaryGenerator();

                            CodeMemberMethod resourcesMethod = new CodeMemberMethod();
                            resourcesMethod.Attributes = MemberAttributes.Static | MemberAttributes.Private;
                            resourcesMethod.Name       = "InitializeElement" + elem.Name + "Resources";
                            resourcesMethod.Parameters.Add(new CodeParameterDeclarationExpression("UIElement", "elem"));
                            classType.Members.Add(resourcesMethod);
                            resourcesGenerator.Generate(elem.Resources, classType, resourcesMethod,
                                                        new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("elem"), "Resources"));

                            method.Statements.Add(new CodeMethodInvokeExpression(null, resourcesMethod.Name, parent));
                        }
                    }

                    IEnumerable children = generator.GetChildren(xamlSource);
                    if (children != null)
                    {
                        foreach (DependencyObject child in children)
                        {
                            if (child == null)
                            {
                                continue;
                            }

                            int            index    = method.Statements.Count;
                            CodeExpression childRef = ProcessGenerators(child, classType, method, generateFields);
                            if (childRef != null)
                            {
                                generator.AddChild(parent, childRef, method, index + 2); // +1 for creating instance +1 for comment
                            }
                        }
                    }

                    BindingGenerator.Instance.ActiveDataType = oldDataType;

                    return(parent);
                }
            }

            string errorText = "Unknown type : " + source.GetType();

            Console.WriteLine(errorText);
            CodeSnippetStatement error = new CodeSnippetStatement("#error " + errorText);

            method.Statements.Add(error);

            return(null);
        }