Example #1
0
        private static CodeExpression GetStyleValueExpression(CodeTypeDeclaration parentClass, CodeMemberMethod method, object value, string baseName, ResourceDictionary dictionary)
        {
            Style  style        = value as Style;
            string variableName = baseName + "_s";
            Type   targetType   = style.TargetType;
            CodeVariableDeclarationStatement styleVar = null;

            if (targetType == typeof(IFrameworkInputElement))
            {
                styleVar   = new CodeVariableDeclarationStatement("Style", variableName, new CodeObjectCreateExpression("Style"));
                targetType = null;
            }
            else
            {
                if (style.BasedOn != null)
                {
                    if (dictionary != null)
                    {
                        object key = FindResourceKey(dictionary, style.BasedOn);
                        if (key == null)
                        {
                            key = style.BasedOn.TargetType;
                        }
                        CodeExpression             keyExpression  = CodeComHelper.GetResourceKeyExpression(key);
                        string                     basedOnVarName = variableName + "_bo";
                        CodeArrayIndexerExpression initExpr       = new CodeArrayIndexerExpression(new CodeThisReferenceExpression(), keyExpression);
                        method.Statements.Add(new CodeVariableDeclarationStatement("var", basedOnVarName, initExpr));

                        styleVar =
                            new CodeVariableDeclarationStatement("Style", variableName,
                                                                 new CodeObjectCreateExpression("Style", new CodeTypeOfExpression(targetType.Name), new CodeSnippetExpression(basedOnVarName + " as Style")));
                    }
                    else
                    {
                        CodeSnippetStatement warning = new CodeSnippetStatement("#warning Style BasedOn is supported only in Dictionary.");
                        method.Statements.Add(warning);
                    }
                }

                if (styleVar == null)
                {
                    styleVar = new CodeVariableDeclarationStatement("Style", variableName, new CodeObjectCreateExpression("Style", new CodeTypeOfExpression(targetType.Name)));
                }
            }

            method.Statements.Add(styleVar);

            int setterIndex = 0;

            foreach (var setterBase in style.Setters)
            {
                Setter setter = setterBase as Setter;
                if (setter != null)
                {
                    string setterVarName = variableName + "_S_" + setterIndex;

                    CodeComHelper.GenerateSetter(parentClass, method, targetType, setter, setterVarName);

                    CodeMethodInvokeExpression addSetter = new CodeMethodInvokeExpression(
                        new CodeVariableReferenceExpression(variableName), "Setters.Add", new CodeVariableReferenceExpression(setterVarName));
                    method.Statements.Add(addSetter);

                    setterIndex++;
                }
            }

            CodeComHelper.GenerateTriggers(parentClass, method, variableName, targetType, style.Triggers);

            return(new CodeVariableReferenceExpression(variableName));
        }