public void ConvertColor_ReturnsSolidColorBrushWithSameColor()
        {
            var color  = Colors.Red;
            var result = _converter.Convert(color, typeof(SolidColorBrush), null, CultureInfo.CurrentCulture);

            Assert.IsType <SolidColorBrush>(result);
            Assert.Equal(color, ((SolidColorBrush)result).Color);
        }
        public void Convert_Null_Null()
        {
            //action
            SolidColorBrush actual = Target.Convert(null, typeof(Brush), null, null) as SolidColorBrush;

            //assert
            Assert.IsNull(actual);
        }
示例#3
0
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            var stack         = serviceProvider.GetService <IAvaloniaXamlIlParentStackProvider>();
            var provideTarget = serviceProvider.GetService <IProvideValueTarget>();

            var targetType = provideTarget.TargetProperty switch
            {
                AvaloniaProperty ap => ap.PropertyType,
                PropertyInfo pi => pi.PropertyType,
                _ => null,
            };

            if (provideTarget.TargetObject is Setter setter)
            {
                targetType = setter.Property.PropertyType;
            }

            // Look upwards though the ambient context for IResourceHosts and IResourceProviders
            // which might be able to give us the resource.
            foreach (var e in stack.Parents)
            {
                object value;

                if (e is IResourceHost host && host.TryGetResource(ResourceKey, out value))
                {
                    return(ColorToBrushConverter.Convert(value, targetType));
                }
        public void ConvertWithNullReturnsNull()
        {
            var converter = new ColorToBrushConverter();

            var value = converter.Convert(null, typeof(string), null, CultureInfo.CurrentCulture);

            Assert.IsNull(value);
        }
        public void ConvertWithNullReturnsNull()
        {
            var converter = new ColorToBrushConverter();

            var value = converter.Convert(null, typeof(string), null, CultureInfo.CurrentCulture);

            Assert.IsNull(value);
        }
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            var stack         = serviceProvider.GetService <IAvaloniaXamlIlParentStackProvider>();
            var provideTarget = serviceProvider.GetService <IProvideValueTarget>();

            var targetType = provideTarget.TargetProperty switch
            {
                AvaloniaProperty ap => ap.PropertyType,
                PropertyInfo pi => pi.PropertyType,
                _ => null,
            };

            if (provideTarget.TargetObject is Setter setter)
            {
                targetType = setter.Property.PropertyType;
            }

            var previousWasControlTheme = false;

            // Look upwards though the ambient context for IResourceNodes
            // which might be able to give us the resource.
            foreach (var parent in stack.Parents)
            {
                if (parent is IResourceNode node && node.TryGetResource(ResourceKey, out var value))
                {
                    return(ColorToBrushConverter.Convert(value, targetType));
                }

                // HACK: Temporary fix for #8678. Hard-coded to only work for the DevTools main
                // window as we don't want 3rd parties to start relying on this hack.
                //
                // We need to implement compile-time merging of resource dictionaries and this
                // hack can be removed.
                if (previousWasControlTheme &&
                    parent is ResourceDictionary hack &&
                    hack.Owner?.GetType().FullName == "Avalonia.Diagnostics.Views.MainWindow" &&
                    hack.Owner.TryGetResource(ResourceKey, out value))
                {
                    return(ColorToBrushConverter.Convert(value, targetType));
                }

                previousWasControlTheme = parent is ControlTheme;
            }

            if (provideTarget.TargetObject is IControl target &&
                provideTarget.TargetProperty is PropertyInfo property)
            {
                // This is stored locally to avoid allocating closure in the outer scope.
                var localTargetType = targetType;
                var localInstance   = this;

                DelayedBinding.Add(target, property, x => localInstance.GetValue(x, localTargetType));
                return(AvaloniaProperty.UnsetValue);
            }

            throw new KeyNotFoundException($"Static resource '{ResourceKey}' not found.");
        }
示例#7
0
        private Func <object?, object?>?GetConverter(AvaloniaProperty targetProperty)
        {
            if (targetProperty?.PropertyType == typeof(IBrush))
            {
                return(x => ColorToBrushConverter.Convert(x, typeof(IBrush)));
            }

            return(null);
        }
示例#8
0
        public void TestConvert1()
        {
            var converter = new ColorToBrushConverter();
            var value     = converter.Convert(Colors.Red, typeof(Brush), null, null);

            value.Should().NotBeNull();
            value.Should().BeOfType <SolidColorBrush>();
            var brush = (SolidColorBrush)value;

            brush.Color.Should().Be(Colors.Red);
        }
        public void ConvertWithColorReturnsBrush()
        {
            var converter = new ColorToBrushConverter();

            var expected = Colors.Blue;

            var actual = converter.Convert(Colors.Blue, typeof(string), null, CultureInfo.CurrentCulture);

            Assert.IsInstanceOfType(actual, typeof(SolidColorBrush));
            Assert.AreEqual(((SolidColorBrush)actual).Color, expected);
        }
        public void ConvertWithColorReturnsBrush()
        {
            var converter = new ColorToBrushConverter();

            var expected = Colors.Blue;

            var actual = converter.Convert(Colors.Blue, typeof(string), null, CultureInfo.CurrentCulture);

            Assert.IsInstanceOfType(actual, typeof(SolidColorBrush));
            Assert.AreEqual(((SolidColorBrush)actual).Color, expected);
        }
示例#11
0
        public void ColorToBrushConverterTest()
        {
            // ReSharper disable PossibleNullReferenceException
            var conv = new ColorToBrushConverter();

            var color = Colors.Red;

            var colorForward = conv.Convert(color, null, null, _cult) as Brush;

            Assert.IsNotNull(colorForward);

            var colorBack = (Color)conv.ConvertBack(colorForward, null, null, _cult);

            Assert.AreEqual(color, colorBack);
            // ReSharper restore PossibleNullReferenceException
        }
示例#12
0
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            var stack         = serviceProvider.GetService <IAvaloniaXamlIlParentStackProvider>();
            var provideTarget = serviceProvider.GetService <IProvideValueTarget>();

            var targetType = provideTarget.TargetProperty switch
            {
                AvaloniaProperty ap => ap.PropertyType,
                PropertyInfo pi => pi.PropertyType,
                _ => null,
            };

            if (provideTarget.TargetObject is Setter setter)
            {
                targetType = setter.Property.PropertyType;
            }

            // Look upwards though the ambient context for IResourceNodes
            // which might be able to give us the resource.
            foreach (var parent in stack.Parents)
            {
                if (parent is IResourceNode node && node.TryGetResource(ResourceKey, out var value))
                {
                    return(ColorToBrushConverter.Convert(value, targetType));
                }
            }

            if (provideTarget.TargetObject is IControl target &&
                provideTarget.TargetProperty is PropertyInfo property)
            {
                // This is stored locally to avoid allocating closure in the outer scope.
                var localTargetType = targetType;
                var localInstance   = this;

                DelayedBinding.Add(target, property, x => localInstance.GetValue(x, localTargetType));
                return(AvaloniaProperty.UnsetValue);
            }

            throw new KeyNotFoundException($"Static resource '{ResourceKey}' not found.");
        }
示例#13
0
        private SolidColorBrush Convert(object toConvert)
        {
            var converter = new ColorToBrushConverter();

            return((SolidColorBrush)converter.Convert(toConvert, typeof(Color), null, CultureInfo.InvariantCulture));
        }
 private object GetValue(IStyledElement control, Type targetType)
 {
     return(ColorToBrushConverter.Convert(control.FindResource(ResourceKey), targetType));
 }
示例#15
0
        public void TestConvert2()
        {
            var converter = new ColorToBrushConverter();

            converter.Convert(null, typeof(Brush), null, null).Should().BeNull();
        }
示例#16
0
        public void Convert_Green()
        {
            var converter = new ColorToBrushConverter();

            Assert.AreEqual(new SolidColorBrush(Colors.Green).ToString(), converter.Convert(Colors.Green, typeof(Brush), null, (CultureInfo)null).ToString());
        }
示例#17
0
        public void Convert_Null()
        {
            var converter = new ColorToBrushConverter();

            Assert.AreEqual(ConverterHelper.UnsetValue, converter.Convert(null, typeof(Brush), null, (CultureInfo)null));
        }