示例#1
0
        public object GetPropertyValue(Type matchedType, TDependencyObject targetObject, string valueExpression, TDependencyProperty property)
        {
            object propertyValue;

            if (valueExpression != null &&
                ((valueExpression.StartsWith("#", StringComparison.Ordinal) && !IsHexColorValue(valueExpression)) ||
                 valueExpression.StartsWith("{", StringComparison.Ordinal))) // color
            {
                if (valueExpression.StartsWith("#"))
                {
                    valueExpression = "{" + valueExpression.Substring(1) + "}";
                }

                propertyValue = markupExpressionParser.ProvideValue(valueExpression, targetObject);
            }
            else
            {
                propertyValue = dependencyPropertyService.GetBindablePropertyValue(matchedType, property, valueExpression);
            }

            return(propertyValue);
        }
示例#2
0
        public override BindableObject CreateTrigger(StyleSheet styleSheet, ITrigger trigger, Type targetType, BindableObject styleResourceReferenceHolder)
        {
            if (trigger == null)
            {
                throw new ArgumentNullException(nameof(trigger));
            }

            if (trigger is Trigger)
            {
                var propertyTrigger = trigger as Trigger;
                var nativeTrigger   = new Xamarin.Forms.Trigger(targetType);

                var bindableProperty = dependencyService.GetBindableProperty(targetType, propertyTrigger.Property);
                if (bindableProperty == null)
                {
                    throw new NullReferenceException($"Property '{propertyTrigger.Property}' may not be null (targetType '{targetType.Name}')!");
                }

                nativeTrigger.Property = bindableProperty;
                nativeTrigger.Value    = dependencyService.GetBindablePropertyValue(targetType, nativeTrigger.Property, propertyTrigger.Value);

                foreach (var styleDeclaration in propertyTrigger.StyleDeclarationBlock)
                {
                    var property = typeNameResolver.GetDependencyProperty(styleSheet.Namespaces, targetType, styleDeclaration.Property);
                    if (property == null)
                    {
                        continue;
                    }
                    try
                    {
                        var value = typeNameResolver.GetPropertyValue(targetType, styleResourceReferenceHolder, styleDeclaration.Value, property, styleSheet.Namespaces);

                        nativeTrigger.Setters.Add(new Setter {
                            Property = property, Value = value
                        });
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in property trigger ""{propertyTrigger.Property} {propertyTrigger.Value} - {styleDeclaration.Property}: {styleDeclaration.Value}"": {e.Message}");
                    }
                }

                foreach (var action in propertyTrigger.EnterActions)
                {
                    try
                    {
                        var nativeTriggerAction = CreateTriggerAction(styleSheet, styleResourceReferenceHolder, action);

                        nativeTrigger.EnterActions.Add(nativeTriggerAction);
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in property trigger ""{propertyTrigger.Property} {propertyTrigger.Value}"" enter action: {e.Message}");
                    }
                }

                foreach (var action in propertyTrigger.ExitActions)
                {
                    try
                    {
                        var nativeTriggerAction = CreateTriggerAction(styleSheet, styleResourceReferenceHolder, action);

                        nativeTrigger.ExitActions.Add(nativeTriggerAction);
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in property trigger ""{propertyTrigger.Property} {propertyTrigger.Value}"" exit action: {e.Message}");
                    }
                }

                return(nativeTrigger);
            }
            else if (trigger is DataTrigger)
            {
                var dataTrigger   = trigger as DataTrigger;
                var nativeTrigger = new Xamarin.Forms.DataTrigger(targetType);

                var expression = "{Binding " + dataTrigger.Binding + "}";

                var binding = (Binding)markupExtensionParser.ProvideValue(expression, null, styleSheet.Namespaces);
                nativeTrigger.Binding = binding;

                nativeTrigger.Value = GetBasicValue(dataTrigger);

                foreach (var styleDeclaration in dataTrigger.StyleDeclarationBlock)
                {
                    try
                    {
                        var property = typeNameResolver.GetDependencyProperty(styleSheet.Namespaces, targetType, styleDeclaration.Property);
                        if (property == null)
                        {
                            continue;
                        }

                        var value = typeNameResolver.GetPropertyValue(targetType, styleResourceReferenceHolder, styleDeclaration.Value, property, styleSheet.Namespaces);

                        nativeTrigger.Setters.Add(new Setter {
                            Property = property, Value = value
                        });
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in data trigger ""{dataTrigger.Binding} {dataTrigger.Value} - {styleDeclaration.Property}: {styleDeclaration.Value}"": {e.Message}");
                    }
                }

                foreach (var action in dataTrigger.EnterActions)
                {
                    try
                    {
                        var nativeTriggerAction = CreateTriggerAction(styleSheet, styleResourceReferenceHolder, action);

                        nativeTrigger.EnterActions.Add(nativeTriggerAction);
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in data trigger ""{dataTrigger.Binding} {dataTrigger.Value} - {action}"" enter action: {e.Message}");
                    }
                }

                foreach (var action in dataTrigger.ExitActions)
                {
                    try
                    {
                        var nativeTriggerAction = CreateTriggerAction(styleSheet, styleResourceReferenceHolder, action);

                        nativeTrigger.ExitActions.Add(nativeTriggerAction);
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in data trigger ""{dataTrigger.Binding} {dataTrigger.Value} - {action}"" exit action: {e.Message}");
                    }
                }

                return(nativeTrigger);
            }
            else if (trigger is EventTrigger)
            {
                var eventTrigger  = trigger as EventTrigger;
                var nativeTrigger = new Xamarin.Forms.EventTrigger();

                nativeTrigger.Event = eventTrigger.Event;

                foreach (var action in eventTrigger.Actions)
                {
                    try
                    {
                        var nativeTriggerAction = CreateTriggerAction(styleSheet, styleResourceReferenceHolder, action);

                        nativeTrigger.Actions.Add(nativeTriggerAction);
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in event trigger ""{eventTrigger.Event} {action.Action}"": {e.Message}");
                    }
                }

                return(nativeTrigger);
            }

            throw new NotSupportedException($"Trigger '{trigger.GetType().FullName}' is not supported!");
        }
示例#3
0
        public override DependencyObject CreateTrigger(StyleSheet styleSheet, ITrigger trigger, Type targetType, DependencyObject styleResourceReferenceHolder)
        {
            if (trigger == null)
            {
                throw new ArgumentNullException(nameof(trigger));
            }

            if (trigger is Trigger)
            {
                var propertyTrigger = trigger as Trigger;
                var nativeTrigger   = new System.Windows.Trigger();

                var dependencyProperty = dependencyService.GetBindableProperty(targetType, propertyTrigger.Property);
                if (dependencyProperty == null)
                {
                    throw new NullReferenceException($"Property '{propertyTrigger.Property}' may not be null (targetType '{targetType.Name}')!");
                }

                nativeTrigger.Property = dependencyProperty;
                nativeTrigger.Value    = dependencyService.GetBindablePropertyValue(targetType, nativeTrigger.Property, propertyTrigger.Value);

                foreach (var styleDeclaration in propertyTrigger.StyleDeclarationBlock)
                {
                    var property = typeNameResolver.GetDependencyProperty(styleSheet.Namespaces, targetType, styleDeclaration.Property);
                    if (property == null)
                    {
                        continue;
                    }
                    try
                    {
                        var value = typeNameResolver.GetPropertyValue(targetType, styleResourceReferenceHolder, styleDeclaration.Value, property, styleSheet.Namespaces);

                        if (value is string valueString)
                        {
                            value = TypeDescriptor.GetConverter(property.PropertyType)?.ConvertFromInvariantString(valueString) ?? value;
                        }

                        nativeTrigger.Setters.Add(new Setter {
                            Property = property, Value = value
                        });
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in property trigger ""{propertyTrigger.Property} {propertyTrigger.Value} - {styleDeclaration.Property}: {styleDeclaration.Value}"": {e.Message}");
                    }
                }

                foreach (var action in propertyTrigger.EnterActions)
                {
                    try
                    {
                        var nativeTriggerAction = CreateTriggerAction(styleSheet, styleResourceReferenceHolder, action);

                        nativeTrigger.EnterActions.Add(nativeTriggerAction);
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in property trigger ""{propertyTrigger.Property} {propertyTrigger.Value}"" enter action: {e.Message}");
                    }
                }

                foreach (var action in propertyTrigger.ExitActions)
                {
                    try
                    {
                        var nativeTriggerAction = CreateTriggerAction(styleSheet, styleResourceReferenceHolder, action);

                        nativeTrigger.ExitActions.Add(nativeTriggerAction);
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in property trigger ""{propertyTrigger.Property} {propertyTrigger.Value}"" exit action: {e.Message}");
                    }
                }

                return(nativeTrigger);
            }
            else if (trigger is DataTrigger)
            {
                var dataTrigger   = trigger as DataTrigger;
                var nativeTrigger = new System.Windows.DataTrigger();

                var expression = "{Binding " + dataTrigger.Binding + "}";

                var binding = (System.Windows.Data.BindingBase)markupExtensionParser.ProvideValue(expression, null, styleSheet.Namespaces);
                nativeTrigger.Binding = binding;

                nativeTrigger.Value = GetBasicValue(dataTrigger);

                foreach (var styleDeclaration in dataTrigger.StyleDeclarationBlock)
                {
                    try
                    {
                        var property = typeNameResolver.GetDependencyProperty(styleSheet.Namespaces, targetType, styleDeclaration.Property);
                        if (property == null)
                        {
                            continue;
                        }

                        var value = typeNameResolver.GetPropertyValue(targetType, styleResourceReferenceHolder, styleDeclaration.Value, property, styleSheet.Namespaces);

                        if (value is string valueString)
                        {
                            value = TypeDescriptor.GetConverter(property.PropertyType)?.ConvertFromInvariantString(valueString) ?? value;
                        }

                        nativeTrigger.Setters.Add(new Setter {
                            Property = property, Value = value
                        });
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in data trigger ""{dataTrigger.Binding} {dataTrigger.Value} - {styleDeclaration.Property}: {styleDeclaration.Value}"": {e.Message}");
                    }
                }

                foreach (var action in dataTrigger.EnterActions)
                {
                    try
                    {
                        var nativeTriggerAction = CreateTriggerAction(styleSheet, styleResourceReferenceHolder, action);

                        nativeTrigger.EnterActions.Add(nativeTriggerAction);
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in data trigger ""{dataTrigger.Binding} {dataTrigger.Value} - {action}"" enter action: {e.Message}");
                    }
                }

                foreach (var action in dataTrigger.ExitActions)
                {
                    try
                    {
                        var nativeTriggerAction = CreateTriggerAction(styleSheet, styleResourceReferenceHolder, action);

                        nativeTrigger.ExitActions.Add(nativeTriggerAction);
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in data trigger ""{dataTrigger.Binding} {dataTrigger.Value} - {action}"" exit action: {e.Message}");
                    }
                }

                return(nativeTrigger);
            }
            else if (trigger is EventTrigger)
            {
                var eventTrigger  = trigger as EventTrigger;
                var nativeTrigger = new System.Windows.EventTrigger();

                var fieldInfo = targetType.GetField(eventTrigger.Event + "Event", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);

                nativeTrigger.RoutedEvent = (RoutedEvent)fieldInfo.GetValue(null);

                foreach (var action in eventTrigger.Actions)
                {
                    try
                    {
                        var nativeTriggerAction = CreateTriggerAction(styleSheet, styleResourceReferenceHolder, action);

                        nativeTrigger.Actions.Add(nativeTriggerAction);
                    }
                    catch (Exception e)
                    {
                        styleSheet.Errors.Add($@"ERROR in event trigger ""{eventTrigger.Event} {action.Action}"": {e.Message}");
                    }
                }

                return(nativeTrigger);
            }

            throw new NotSupportedException($"Trigger '{trigger.GetType().FullName}' is not supported!");
        }
示例#4
0
        public override DependencyObject CreateTrigger(StyleSheet styleSheet, ITrigger trigger, Type targetType, DependencyObject styleResourceReferenceHolder)
        {
            if (trigger == null)
            {
                throw new ArgumentNullException(nameof(trigger));
            }

            if (trigger is Trigger)
            {
                var propertyTrigger = trigger as Trigger;
                var nativeTrigger   = new System.Windows.Trigger();

                var dependencyProperty = dependencyService.GetBindableProperty(targetType, propertyTrigger.Property);
                if (dependencyProperty == null)
                {
                    throw new NullReferenceException($"Property '{propertyTrigger.Property}' may not be null (targetType '{targetType.Name}')!");
                }

                nativeTrigger.Property = dependencyProperty;
                nativeTrigger.Value    = dependencyService.GetBindablePropertyValue(targetType, nativeTrigger.Property, propertyTrigger.Value);

                foreach (var styleDeclaration in propertyTrigger.StyleDeclaraionBlock)
                {
                    var property = typeNameResolver.GetDependencyProperty(styleSheet.Namespaces, targetType, styleDeclaration.Property);
                    if (property == null)
                    {
                        continue;
                    }
                    var value = typeNameResolver.GetPropertyValue(targetType, styleResourceReferenceHolder, styleDeclaration.Value, property);

                    if (value is string valueString)
                    {
                        value = TypeDescriptor.GetConverter(property.PropertyType)?.ConvertFromInvariantString(valueString) ?? value;
                    }

                    nativeTrigger.Setters.Add(new Setter {
                        Property = property, Value = value
                    });
                }

                return(nativeTrigger);
            }
            else if (trigger is DataTrigger)
            {
                var dataTrigger   = trigger as DataTrigger;
                var nativeTrigger = new System.Windows.DataTrigger();

                var expression = "{Binding " + dataTrigger.Binding + "}";

                var binding = (System.Windows.Data.BindingBase)markupExtensionParser.ProvideValue(expression, null);
                nativeTrigger.Binding = binding;

                nativeTrigger.Value = GetBasicValue(dataTrigger);

                foreach (var styleDeclaration in dataTrigger.StyleDeclarationBlock)
                {
                    var property = typeNameResolver.GetDependencyProperty(styleSheet.Namespaces, targetType, styleDeclaration.Property);
                    if (property == null)
                    {
                        continue;
                    }

                    var value = typeNameResolver.GetPropertyValue(targetType, styleResourceReferenceHolder, styleDeclaration.Value, property);

                    if (value is string valueString)
                    {
                        value = TypeDescriptor.GetConverter(property.PropertyType)?.ConvertFromInvariantString(valueString) ?? value;
                    }

                    nativeTrigger.Setters.Add(new Setter {
                        Property = property, Value = value
                    });
                }

                return(nativeTrigger);
            }
            else if (trigger is EventTrigger)
            {
                var eventTrigger  = trigger as EventTrigger;
                var nativeTrigger = new System.Windows.EventTrigger();

                var fieldInfo = targetType.GetField(eventTrigger.Event + "Event", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);

                nativeTrigger.RoutedEvent = (RoutedEvent)fieldInfo.GetValue(null);

                foreach (var action in eventTrigger.Actions)
                {
                    var actionTypeName = typeNameResolver.ResolveFullTypeName(styleSheet.Namespaces, action.Action);
                    var actionType     = Type.GetType(actionTypeName);
                    var triggerAction  = (System.Windows.TriggerAction)Activator.CreateInstance(actionType);

                    var parameters = action.Parameters.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                     .Select(x => x.Trim())
                                     .ToList();

                    foreach (var parameter in parameters)
                    {
                        var                parameterName            = parameter.Split(' ')[0];
                        object             value                    = null;
                        var                parameterValueExpression = parameter.Substring(parameterName.Length + 1).Trim();
                        DependencyProperty depProp;
                        var                type = typeNameResolver.GetClrPropertyType(styleSheet.Namespaces, triggerAction, parameterName);

                        if (typeNameResolver.IsMarkupExtension(parameterValueExpression))
                        {
                            value = typeNameResolver.GetMarkupExtensionValue(styleResourceReferenceHolder, parameterValueExpression);
                        }
                        else if ((depProp = typeNameResolver.GetDependencyProperty(styleSheet.Namespaces, actionType, parameterName)) != null)
                        {
                            value = typeNameResolver.GetPropertyValue(actionType, styleResourceReferenceHolder, parameterValueExpression, depProp);

                            if (value is DynamicResourceExtension)
                            {
                                var dyn             = value as DynamicResourceExtension;
                                var serviceProvider = (IServiceProvider)typeof(Application).GetProperty("ServiceProvider", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Application.Current);
                                value = dyn.ProvideValue(serviceProvider);
                            }
                            else if (value is StaticResourceExtension)
                            {
                                var dyn             = value as StaticResourceExtension;
                                var serviceProvider = (IServiceProvider)typeof(Application).GetProperty("ServiceProvider", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Application.Current);
                                value = dyn.ProvideValue(serviceProvider);
                            }
                        }
                        else
                        {
                            value = parameterValueExpression;
                        }

                        if (value is string valueString)
                        {
                            value = TypeDescriptor.GetConverter(type)?.ConvertFromInvariantString(valueString) ?? value;
                        }

                        triggerAction.GetType().GetRuntimeProperty(parameterName).SetValue(triggerAction, value);
                    }

                    nativeTrigger.Actions.Add(triggerAction);
                }

                return(nativeTrigger);
            }

            throw new NotSupportedException($"Trigger '{trigger.GetType().FullName}' is not supported!");
        }
示例#5
0
        private Dictionary <TDependencyProperty, object> CreateStyleDictionaryFromDeclarationBlock(
            List <CssNamespace> namespaces,
            StyleDeclarationBlock declarationBlock,
            Type matchedType,
            TDependencyObject dependencyObject)
        {
            var propertyStyleValues = new Dictionary <TDependencyProperty, object>();

            foreach (var i in declarationBlock)
            {
                TDependencyProperty property;

                if (i.Property.Contains("."))
                {
                    string typename     = null;
                    string propertyName = null;

                    if (i.Property.Contains("|"))
                    {
                        var strs  = i.Property.Split('|', '.');
                        var alias = strs[0];
                        var namespaceFragments = namespaces
                                                 .First(x => x.Alias == alias)
                                                 .Namespace
                                                 .Split(',');

                        typename     = $"{namespaceFragments[0]}.{strs[1]}, {string.Join(",", namespaceFragments.Skip(1))}";
                        propertyName = strs[2];
                    }
                    else
                    {
                        var strs = i.Property.Split('.');
                        var namespaceFragments = namespaces
                                                 .First(x => x.Alias == "")
                                                 .Namespace
                                                 .Split(',');

                        typename     = $"{namespaceFragments[0]}.{strs[0]}, {string.Join(",", namespaceFragments.Skip(1))}";
                        propertyName = strs[1];
                    }

                    property = dependencyPropertyService.GetBindableProperty(Type.GetType(typename), propertyName);
                }
                else
                {
                    property = dependencyPropertyService.GetBindableProperty(matchedType, i.Property);
                }

                if (property == null)
                {
                    continue;
                }

                object propertyValue = null;
                if (i.Value is string &&
                    ((string)i.Value).StartsWith("{", StringComparison.Ordinal))
                {
                    propertyValue = markupExpressionParser.ProvideValue((string)i.Value, dependencyObject);
                }
                else
                {
                    propertyValue = dependencyPropertyService.GetBindablePropertyValue(matchedType, property, i.Value);
                }

                propertyStyleValues[property] = propertyValue;
            }

            return(propertyStyleValues);
        }