示例#1
0
        private CreateStyleDictionaryFromDeclarationBlockResult <TDependencyProperty> CreateStyleDictionaryFromDeclarationBlock(
            List <CssNamespace> namespaces,
            StyleDeclarationBlock declarationBlock,
            Type matchedType,
            TDependencyObject dependencyObject)
        {
            var result = new CreateStyleDictionaryFromDeclarationBlockResult <TDependencyProperty>();

            foreach (var styleDeclaration in declarationBlock)
            {
                var property = cssTypeHelper.GetDependencyProperty(namespaces, matchedType, styleDeclaration.Property);

                if (property == null)
                {
                    continue;
                }

                try
                {
                    var propertyValue = cssTypeHelper.GetPropertyValue(matchedType, dependencyObject, styleDeclaration.Value, property, namespaces);

                    result.PropertyStyleValues[property] = propertyValue;
                }
                catch
                {
                    result.Errors.Add($"Cannot get property-value for '{styleDeclaration.Property}' with value '{styleDeclaration.Value}'!");
                }
            }

            return(result);
        }
示例#2
0
        private static void GenerateStyles(StyleSheet styleSheet,
                                           IDictionary <TDependencyObject, StyleUpdateInfo> styleMatchInfos,
                                           IStyleResourcesService applicationResourcesService,
                                           IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService,
                                           INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService,
                                           CssTypeHelper <TDependencyObject, TDependencyProperty, TStyle> cssTypeHelper)
        {
            applicationResourcesService.EnsureResources();

            foreach (var styleMatchInfoKeyValue in styleMatchInfos)
            {
                var styleMatchInfo = styleMatchInfoKeyValue.Value;

                var matchedElementType = styleMatchInfo.MatchedType;

                for (var i = 0; i < styleMatchInfo.CurrentMatchedSelectors.Count; i++)
                {
                    var selector    = styleMatchInfo.CurrentMatchedSelectors.ElementAt(i);
                    var resourceKey = styleMatchInfo.CurrentMatchedResourceKeys.ElementAt(i);

                    if (applicationResourcesService.Contains(resourceKey))
                    {
                        // Debug.WriteLine($"GenerateStyles: Already contains '{s}' ({matchedElementType.Name})");
                        continue;
                    }

                    // Debug.WriteLine($"GenerateStyles: Generating '{s}' ({matchedElementType.Name})");

                    var rule = styleMatchInfo.CurrentStyleSheet.Rules.Where(x => x.SelectorString == selector.Value).First();

                    CreateStyleDictionaryFromDeclarationBlockResult <TDependencyProperty> result = null;
                    try
                    {
                        result = CreateStyleDictionaryFromDeclarationBlock(
                            styleSheet.Namespaces,
                            rule.DeclarationBlock,
                            matchedElementType,
                            (TDependencyObject)styleSheet.AttachedTo,
                            cssTypeHelper);

                        var propertyStyleValues = result.PropertyStyleValues;

                        foreach (var error in result.Errors)
                        {
                            // Debug.WriteLine($@" ERROR (normal) in Selector ""{rule.SelectorString}"": {error}");
                            styleSheet.AddError($@"ERROR in Selector ""{rule.SelectorString}"": {error}");
                        }

                        var nativeTriggers = rule.DeclarationBlock.Triggers
                                             .Select(x => nativeStyleService.CreateTrigger(styleSheet, x, styleMatchInfo.MatchedType, (TDependencyObject)styleSheet.AttachedTo))
                                             .ToList();

                        var initalStyle = dependencyPropertyService.GetInitialStyle(styleMatchInfoKeyValue.Key);
                        if (initalStyle != null)
                        {
                            var subDict = nativeStyleService.GetStyleAsDictionary(initalStyle as TStyle);

                            foreach (var item in subDict)
                            {
                                // only set not-overridden properties
                                if (!propertyStyleValues.ContainsKey(item.Key))
                                {
                                    propertyStyleValues[item.Key] = item.Value;
                                }
                            }

                            var triggers = nativeStyleService.GetTriggersAsList(initalStyle as TStyle);
                            nativeTriggers.InsertRange(0, triggers);
                        }
                        //Debug.WriteLine("    Values: " + string.Join(", ", propertyStyleValues.Select(x => ((dynamic)x.Key).PropertyName + ": " + x.Value.ToString())));
                        var style = nativeStyleService.CreateFrom(propertyStyleValues, nativeTriggers, matchedElementType);

                        applicationResourcesService.SetResource(resourceKey, style);

                        // Debug.WriteLine("Finished generate Style " + resourceKey);
                    }
                    catch (Exception e)
                    {
                        // Debug.WriteLine($@" ERROR (exception) in Selector ""{rule.SelectorString}"": {e.Message}");
                        styleSheet.AddError($@"ERROR in Selector ""{rule.SelectorString}"": {e.Message}");
                    }
                }
            }
        }
示例#3
0
        private void GenerateStyles(TUIElement styleResourceReferenceHolder, StyleSheet styleSheet, TUIElement startFrom, List <StyleMatchInfo> styleMatchInfos)
        {
            applicationResourcesService.EnsureResources();

            foreach (var styleMatchInfo in styleMatchInfos)
            {
                var matchedElementType = styleMatchInfo.MatchedType;

                var resourceKey = nativeStyleService.GetStyleResourceKey(styleSheet.Id, matchedElementType, styleMatchInfo.Rule.SelectorString);

                if (applicationResourcesService.Contains(resourceKey))
                {
                    continue;
                }

                CreateStyleDictionaryFromDeclarationBlockResult <TDependencyProperty> result = null;
                try
                {
                    result = CreateStyleDictionaryFromDeclarationBlock(
                        styleSheet.Namespaces,
                        styleMatchInfo.Rule.DeclarationBlock,
                        matchedElementType,
                        startFrom ?? styleResourceReferenceHolder);

                    var propertyStyleValues = result.PropertyStyleValues;

                    foreach (var error in result.Errors)
                    {
                        styleSheet.AddError($@"ERROR in Selector ""{styleMatchInfo.Rule.SelectorString}"": {error}");
                    }

                    var nativeTriggers = styleMatchInfo.Rule.DeclarationBlock.Triggers
                                         .Select(x => nativeStyleService.CreateTrigger(styleSheet, x, styleMatchInfo.MatchedType, styleResourceReferenceHolder));

                    if (propertyStyleValues.Keys.Count == 0)
                    {
                        continue;
                    }

                    var style = nativeStyleService.CreateFrom(propertyStyleValues, nativeTriggers, matchedElementType);

                    applicationResourcesService.SetResource(resourceKey, style);
                }
                catch (Exception e)
                {
                    styleSheet.AddError($@"ERROR in Selector ""{styleMatchInfo.Rule.SelectorString}"": {e.Message}");
                }
            }
        }
示例#4
0
        private static void GenerateStyles(StyleSheet styleSheet,
                                           IDictionary <TDependencyObject, StyleUpdateInfo> styleMatchInfos,
                                           IStyleResourcesService applicationResourcesService,
                                           IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService,
                                           INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService,
                                           CssTypeHelper <TDependencyObject, TDependencyProperty, TStyle> cssTypeHelper)
        {
            applicationResourcesService.EnsureResources();

            foreach (var styleMatchInfoKeyValue in styleMatchInfos)
            {
                var styleMatchInfo = styleMatchInfoKeyValue.Value;

                var matchedElementType = styleMatchInfo.MatchedType;

                foreach (var resourceKey in styleMatchInfo.CurrentMatchedSelectors)
                {
                    if (applicationResourcesService.Contains(resourceKey))
                    {
                        continue;
                    }

                    var s = resourceKey.Split('{')[1];

                    var rule = styleMatchInfo.CurrentStyleSheet.Rules.Where(x => x.SelectorString == s).First();
                    // // Debug.WriteLine("Generate Style " + resourceKey);

                    CreateStyleDictionaryFromDeclarationBlockResult <TDependencyProperty> result = null;
                    try
                    {
                        result = "CreateStyleDictionaryFromDeclarationBlock".Measure(() => CreateStyleDictionaryFromDeclarationBlock(
                                                                                         styleSheet.Namespaces,
                                                                                         rule.DeclarationBlock,
                                                                                         matchedElementType,
                                                                                         (TDependencyObject)styleSheet.AttachedTo,
                                                                                         cssTypeHelper));

                        var propertyStyleValues = result.PropertyStyleValues;

                        foreach (var error in result.Errors)
                        {
                            Debug.WriteLine($@" ERROR (normal) in Selector ""{rule.SelectorString}"": {error}");
                            styleSheet.AddError($@"ERROR in Selector ""{rule.SelectorString}"": {error}");
                        }

                        var nativeTriggers = $"CreateTriggers ({rule.DeclarationBlock.Triggers.Count})".Measure(() => rule.DeclarationBlock.Triggers
                                                                                                                .Select(x => nativeStyleService.CreateTrigger(styleSheet, x, styleMatchInfo.MatchedType, (TDependencyObject)styleSheet.AttachedTo))
                                                                                                                .ToList());


                        var initalStyle = dependencyPropertyService.GetInitialStyle(styleMatchInfoKeyValue.Key);
                        if (initalStyle != null)
                        {
                            var subDict = nativeStyleService.GetStyleAsDictionary(initalStyle as TStyle);

                            foreach (var i in subDict)
                            {
                                propertyStyleValues[i.Key] = i.Value;
                            }

                            var triggers = nativeStyleService.GetTriggersAsList(initalStyle as TStyle);
                            nativeTriggers.AddRange(triggers);
                        }

                        foreach (var item in propertyStyleValues)
                        {
                            if (item.Value == null)
                            {
                            }
                        }

                        var style = "Create Style".Measure(() => nativeStyleService.CreateFrom(propertyStyleValues, nativeTriggers, matchedElementType));

                        applicationResourcesService.SetResource(resourceKey, style);

                        // // Debug.WriteLine("Finished generate Style " + resourceKey);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine($@" ERROR (exception) in Selector ""{rule.SelectorString}"": {e.Message}");
                        styleSheet.AddError($@"ERROR in Selector ""{rule.SelectorString}"": {e.Message}");
                    }
                }
            }
        }