/// <inheritdoc/>
        public void OnNext(object value)
        {
            using (_inner.Subscribe(_ => { }))
            {
                var type = _inner.ResultType;

                if (type != null)
                {
                    var converted = Converter.ConvertBack(
                        value,
                        type,
                        ConverterParameter,
                        CultureInfo.CurrentCulture);

                    if (converted == AvaloniaProperty.UnsetValue)
                    {
                        converted = TypeUtilities.Default(type);
                        _inner.SetValue(converted, _priority);
                    }
                    else if (converted is BindingNotification)
                    {
                        var notification = converted as BindingNotification;

                        if (notification.ErrorType == BindingErrorType.None)
                        {
                            throw new AvaloniaInternalException(
                                      "IValueConverter should not return non-errored BindingNotification.");
                        }

                        _errors.OnNext(notification);

                        if (_fallbackValue != AvaloniaProperty.UnsetValue)
                        {
                            if (TypeUtilities.TryConvert(
                                    type,
                                    _fallbackValue,
                                    CultureInfo.InvariantCulture,
                                    out converted))
                            {
                                _inner.SetValue(converted, _priority);
                            }
                            else
                            {
                                Logger.Error(
                                    LogArea.Binding,
                                    this,
                                    "Could not convert FallbackValue {FallbackValue} to {Type}",
                                    _fallbackValue,
                                    type);
                            }
                        }
                    }
                    else
                    {
                        _inner.SetValue(converted, _priority);
                    }
                }
            }
        }
Beispiel #2
0
        /// <inheritdoc/>
        public void OnNext(object value)
        {
            var type = _inner.ResultType;

            if (type != null)
            {
                var converted = Converter.ConvertBack(
                    value,
                    type,
                    ConverterParameter,
                    CultureInfo.CurrentUICulture);

                if (converted == AvaloniaProperty.UnsetValue)
                {
                    converted = TypeUtilities.Default(type);
                    _inner.SetValue(converted, _priority);
                }
                else if (converted is BindingError)
                {
                    var error = converted as BindingError;

                    Logger.Error(
                        LogArea.Binding,
                        this,
                        "Error binding to {Expression}: {Message}",
                        _inner.Expression,
                        error.Exception.Message);

                    if (_fallbackValue != null)
                    {
                        if (TypeUtilities.TryConvert(
                                type,
                                _fallbackValue,
                                CultureInfo.InvariantCulture,
                                out converted))
                        {
                            _inner.SetValue(converted, _priority);
                        }
                        else
                        {
                            Logger.Error(
                                LogArea.Binding,
                                this,
                                "Could not convert FallbackValue {FallbackValue} to {Type}",
                                _fallbackValue,
                                type);
                        }
                    }
                }
                else
                {
                    _inner.SetValue(converted, _priority);
                }
            }
        }