Beispiel #1
0
#pragma warning restore CS1998

        private async Task HandleErrorAsync(ConversionError error, string sourceUnit)
        {
            SetMemberConfig(Context.User.Id);
            var cvtCmd    = _cvtCommandTexts[Context.User.Id];
            var cvtErrors = cvtCmd["errors"] as Dictionary <string, object>;

            var lengthTooShort = cvtErrors["length_too_short"].ToString()
                                 .Replace("{temps}", string.Join(", ", _temps))
                                 .Replace("{heights}", string.Join(", ", _lengths))
                                 .Replace("{prefix}", DotNetEnv.Env.GetString("PREFIX"));

            var invalidUnitMsg = cvtErrors["invalid_unit"].ToString()
                                 .Replace("{units}", string.Join(", ", _validUnits));

            var wrongPatterns = cvtErrors["wrong_pattern"].ToString()
                                .Replace("{input}", sourceUnit);

            string msg = error switch
            {
                ConversionError.LengthTooShort => lengthTooShort,
                ConversionError.InvalidUnit => invalidUnitMsg,
                ConversionError.WrongPattern => wrongPatterns,
                _ => string.Empty
            };

            await SendMessageAndDeleteAfterTimeout(Context, msg, _timeOut);
        }
Beispiel #2
0
 /// <summary>
 /// Recreates a binding conversion error to a control.
 /// </summary>
 /// <param name="e">The e.</param>
 public void AttachError(ConversionError e)
 {
     foreach (var er in this.elementHandlers)
     {
         er.CreateValError(e);
     }
 }
Beispiel #3
0
        private void HandleDataConversionError(object sender, ValidationErrorEventArgs args)
        {
            if (this.reattachErrorCommand.ActiveWaiting)
            {
                return;
            }

            Console.WriteLine("Conversion error {0}" + args.Action);
            var bindingTargetElement = args.OriginalSource as FrameworkElement;
            var erroredExpression = args.Error.BindingInError as BindingExpression;

            if (erroredExpression == null || bindingTargetElement == null)
            {
                return;
            }

            var bindingPath = erroredExpression.ParentBinding.Path.Path;

            if (args.Action.Equals(ValidationErrorEventAction.Added))
            {
                var bindingProperty = ValidationProperties.GetBoundProperty(bindingTargetElement);
                var badData = bindingTargetElement.GetValue(bindingProperty);
                var conversionError = new ConversionError(bindingPath, string.Format("Invalid format {0}", bindingProperty), badData);
                this.errorSource.Add(conversionError);

                var props = TypeDescriptor.GetProperties(erroredExpression.DataItem);
                var p = props.Find(bindingPath, false);
                if (p != null)
                {
                    var wdb = erroredExpression.ParentBinding as WatchdogBinding;
                    if (wdb != null)
                    {
                        if (wdb.ResetOnConvertFailure)
                        {
                            wdb.SuspendTransfer = true;
                            p.SetValue(erroredExpression.DataItem, TypeData.DefaultValue(p.PropertyType));
                            wdb.SuspendTransfer = false;
                        }
                    }
                }
            }
            else if (args.Action.Equals(ValidationErrorEventAction.Removed))
            {
                foreach (var err in this.errorSource.OfType<ConversionError>().MatchingField(bindingPath).ToList())
                {
                    this.errorSource.Remove(err);
                }
            }
        }
        /// <summary>
        /// Creates the val error.
        /// </summary>
        /// <param name="e">The e.</param>
        public void CreateValError(ConversionError e)
        {
            Debug.WriteLine("Reattaching conversion error");

            var t = this.controlRef.Target as FrameworkElement;

            if (t != null)
            {
                var prop = ValidationProperties.GetBoundProperty(t);
                var be = BindingOperations.GetBindingExpression(t, prop);

                if (prop != null && be != null)
                {
                    t.SetValue(prop, e.InvalidData);
                    be.UpdateSource();
                }
            }
        }