Пример #1
0
        /// <summary>
        /// This method is used to check whether the value is Valid if needed.
        /// </summary>
        /// <param name="initialValue"></param>
        internal void CheckInitialValueValidity(object initialValue)
        {
            if (ParentBinding.ValidatesOnExceptions && ParentBinding.ValidatesOnLoad)
            {
                if (!_propertyPathWalker.IsPathBroken)
                {
                    INTERNAL_ForceValidateOnNextSetValue = false;
                    try
                    {
                        IPropertyPathNode node = _propertyPathWalker.FinalNode;
                        node.SetValue(node.Value); //we set the source property to its own value to check whether it causes an exception, in which case the value is not valid.
                    }
                    catch (Exception e)            //todo: put the content of this catch in a method which will be called here and in UpdateSourceObject (OR put the whole try/catch in the method and put the Value to set as parameter).
                    {
                        //We get the new Error (which is the innermost exception as far as I know):
                        Exception currentException = e;
#if OPENSILVER
                        if (true)                                    // Note: "InnerException" is only supported in the Simulator as of July 27, 2017.
#elif BRIDGE
                        if (CSHTML5.Interop.IsRunningInTheSimulator) // Note: "InnerException" is only supported in the Simulator as of July 27, 2017.
#endif
                        {
                            while (currentException.InnerException != null)
                            {
                                currentException = currentException.InnerException;
                            }
                        }

                        Validation.MarkInvalid(this, new ValidationError(this)
                        {
                            Exception = currentException, ErrorContent = currentException.Message
                        });
                    }
                }
            }
        }
Пример #2
0
        internal void UpdateSourceObject(object value)
        {
            if (_propertyPathWalker.IsPathBroken)
            {
                return;
            }

            IPropertyPathNode node = _propertyPathWalker.FinalNode;
            bool oldIsUpdating     = IsUpdating;

            object convertedValue = value;
            Type   expectedType   = node.Type;

            try
            {
                if (expectedType != null && ParentBinding.Converter != null)
                {
#if MIGRATION
                    convertedValue = ParentBinding.Converter.ConvertBack(value, expectedType, ParentBinding.ConverterParameter, ParentBinding.ConverterCulture);
#else
                    convertedValue = ParentBinding.Converter.ConvertBack(value, expectedType, ParentBinding.ConverterParameter, ParentBinding.ConverterLanguage);
#endif

                    if (convertedValue == DependencyProperty.UnsetValue)
                    {
                        return;
                    }
                }

                if (!IsValueValidForSourceUpdate(convertedValue, expectedType))
                {
                    IsUpdating = true;

#if MIGRATION
                    convertedValue = DynamicConverter.Convert(convertedValue, expectedType, null, ParentBinding.ConverterCulture);
#else
                    convertedValue = DynamicConverter.Convert(convertedValue, expectedType, null, ParentBinding.ConverterLanguage);
#endif

                    if (convertedValue == DependencyProperty.UnsetValue)
                    {
                        return;
                    }
                }

                node.SetValue(convertedValue);
                Validation.ClearInvalid(this);
            }
            catch (Exception e)
            {
                //If we have ValidatesOnExceptions set to true, we display a popup with the error close to the element.
                if (ParentBinding.ValidatesOnExceptions)
                {
                    //We get the new Error (which is the innermost exception as far as I know):
                    Exception currentException = e;

                    while (currentException.InnerException != null)
                    {
                        currentException = currentException.InnerException;
                    }

                    Validation.MarkInvalid(this, new ValidationError(this)
                    {
                        Exception = currentException, ErrorContent = currentException.Message
                    });
                }
            }
            finally
            {
                IsUpdating = oldIsUpdating;
            }
        }