示例#1
0
        protected override bool TryParseValueFromString(string?value, out TEnum result, out string validationErrorMessage)
        {
            // Let's Blazor convert the value for us 😊
            if (BindConverter.TryConvertTo(value, CultureInfo.CurrentCulture, out TEnum parsedValue))
            {
                result = parsedValue;
                validationErrorMessage = "";
                return(true);
            }

            // Map null/empty value to null if the bound object is nullable
            if (string.IsNullOrEmpty(value))
            {
                var nullableType = Nullable.GetUnderlyingType(typeof(TEnum));
                if (nullableType != null)
                {
                    result = default;
                    validationErrorMessage = "";
                    return(true);
                }
            }

            // The value is invalid => set the error message
            result = default;
            validationErrorMessage = $"The {FieldIdentifier.FieldName} field is not valid.";
            return(false);
        }
示例#2
0
        /// <summary>
        /// 将 字符串 Value 属性转化为 泛型 Value 方法
        /// </summary>
        /// <param name="value"></param>
        /// <param name="result"></param>
        /// <param name="validationErrorMessage"></param>
        /// <returns></returns>
        protected override bool TryParseValueFromString(string value, out TItem result, out string?validationErrorMessage)
        {
            if (typeof(TItem) == typeof(string))
            {
                result = (TItem)(object)value;
                validationErrorMessage = null;
                return(true);
            }
            else if (typeof(TItem).IsEnum)
            {
                var success = BindConverter.TryConvertTo <TItem>(value, CultureInfo.CurrentCulture, out var parsedValue);
                if (success)
                {
                    result = parsedValue;
                    validationErrorMessage = null;
                    return(true);
                }
                else
                {
#nullable disable
                    result = default;
#nullable restore
                    if (FieldIdentifier != null)
                    {
                        validationErrorMessage = $"The {FieldIdentifier.Value.FieldName} field is not valid.";
                        return(false);
                    }
                    else
                    {
                        validationErrorMessage = null;
                        return(true);
                    }
                }
        protected void OnInput(ChangeEventArgs args, int index = 0)
        {
            if (index != 0)
            {
                throw new ArgumentOutOfRangeException("DatePicker should have only single picker.");
            }
            if (args == null)
            {
                return;
            }

            if (BindConverter.TryConvertTo(args.Value.ToString(), CultureInfo, out TValue changeValue))
            {
                if (Picker == DatePickerType.Date)
                {
                    if (IsDateStringFullDate(args.Value.ToString()))
                    {
                        CurrentValue = changeValue;
                    }
                }
                else
                {
                    CurrentValue = changeValue;
                }

                GetIfNotNull(changeValue, (notNullValue) =>
                {
                    PickerValues[0] = notNullValue;
                });

                StateHasChanged();
            }

            UpdateCurrentValueAsString();
        }
        protected override bool TryParseValueFromString(string value, out string result, out string validationErrorMessage)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                if (DefaultToEmptyString)
                {
                    result = string.Empty;
                }
                else
                {
                    result = default;
                }
                validationErrorMessage = null;
                return(true);
            }

            var success = BindConverter.TryConvertTo <string>(
                value, CultureInfo.CurrentCulture, out var parsedValue);

            if (success)
            {
                result = parsedValue;
                validationErrorMessage = null;

                return(true);
            }
            else
            {
                result = default;
                validationErrorMessage = $"{FieldIdentifier.FieldName} field isn't valid.";

                return(false);
            }
        }
        /// <summary>
        /// Method is called via EventCallBack if the keyboard key is no longer pressed inside the Input element.
        /// </summary>
        /// <param name="e">Contains the key (combination) which was pressed inside the Input element</param>
        protected async Task OnKeyUp(KeyboardEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }

            var key = e.Key.ToUpperInvariant();

            if (key == "ENTER")
            {
                if (string.IsNullOrWhiteSpace(_inputStart.Value))
                {
                    ClearValue();
                }
                else
                {
                    if (BindConverter.TryConvertTo(_inputStart.Value, CultureInfo, out TValue changeValue))
                    {
                        Value = changeValue;
                    }
                    Close();
                }
            }

            if (key == "ARROWDOWN" && !_dropDown.IsOverlayShow())
            {
                await _dropDown.Show();
            }
            if (key == "ARROWUP" && _dropDown.IsOverlayShow())
            {
                Close();
            }
        }
示例#6
0
        /// <summary>
        /// 将 字符串 Value 属性转化为 泛型 Value 方法
        /// </summary>
        /// <param name="value"></param>
        /// <param name="result"></param>
        /// <param name="validationErrorMessage"></param>
        /// <returns></returns>
        protected override bool TryParseValueFromString(string?value, [MaybeNullWhen(false)] out TItem result, [NotNullWhen(false)] out string?validationErrorMessage)
        {
            if (!string.IsNullOrEmpty(value) && typeof(TItem) == typeof(string))
            {
                result = (TItem)(object)value;
                validationErrorMessage = null;
                return(true);
            }
            else if (typeof(TItem).IsEnum)
            {
                var success = BindConverter.TryConvertTo <TItem>(value, CultureInfo.CurrentCulture, out var parsedValue);
                if (success)
                {
                    result = parsedValue !;
                    validationErrorMessage = null;
                    return(true);
                }
                else
                {
                    result = default;
                    validationErrorMessage = $"The {FieldIdentifier.FieldName} field is not valid.";
                    return(false);
                }
            }
            else if (typeof(TItem).IsValueType)
            {
                result = (TItem)Convert.ChangeType(value, typeof(TItem)) !;
                validationErrorMessage = null;
                return(true);
            }

            throw new InvalidOperationException($"{GetType()} does not support the type '{typeof(TItem)}'.");
        }
        public override void ChangeValue(DateTime value, int index = 0)
        {
            bool result = BindConverter.TryConvertTo <TValue>(
                value.ToString(CultureInfo), CultureInfo, out var dateTime);

            if (result)
            {
                Value = dateTime;
            }

            _pickerStatus[index]._hadSelectValue = true;

            UpdateCurrentValueAsString();

            if (IsRange && !IsShowTime && Picker != DatePickerType.Time)
            {
                if (_pickerStatus[0]._hadSelectValue && _pickerStatus[1]._hadSelectValue)
                {
                    Close();
                }
            }
            else if (!IsShowTime && Picker != DatePickerType.Time)
            {
                Close();
            }

            if (OnChange.HasDelegate)
            {
                OnChange.InvokeAsync(new DateTimeChangedEventArgs
                {
                    Date       = value,
                    DateString = GetInputValue(index)
                });
            }
        }
示例#8
0
 /// <inheritdoc />
 /// <summary>
 /// Override Method.
 /// Handles constraint checking - min/max and decimal places if enabled
 /// </summary>
 protected override bool TryParseValueFromString(string value, out TValue result, out string validationErrorMessage)
 {
     if (BindConverter.TryConvertTo <TValue>(value, CultureInfo.InvariantCulture, out result))
     {
         if (this.ApplyConstraints)
         {
             validationErrorMessage = null;
             if (this.CheckValueAgainstConstraints(result))
             {
                 return(true);
             }
             else
             {
                 validationErrorMessage = $"The {FieldIdentifier.FieldName} entered value is not within the constraints {this.MinValue} and {this.MaxValue}";
                 return(false);
             }
         }
         validationErrorMessage = null;
         return(true);
     }
     else
     {
         validationErrorMessage = string.Format(ParsingErrorMessage, FieldIdentifier.FieldName);
         return(false);
     }
 }
示例#9
0
        public override void ChangeValue(DateTime value, int index = 0)
        {
            if (index != 0)
            {
                throw new ArgumentOutOfRangeException("DatePicker should have only single picker.");
            }
            UseDefaultPickerValue[0] = false;
            bool result = BindConverter.TryConvertTo <TValue>(
                value.ToString(CultureInfo), CultureInfo, out var dateTime);

            if (result)
            {
                CurrentValue = dateTime;
            }

            _pickerStatus[0]._hadSelectValue = true;

            if (!IsShowTime && Picker != DatePickerType.Time)
            {
                Close();
            }

            if (OnChange.HasDelegate)
            {
                OnChange.InvokeAsync(new DateTimeChangedEventArgs
                {
                    Date       = value,
                    DateString = GetInputValue(0)
                });
            }
        }
示例#10
0
        protected override bool TryParseValueFromString(string value, out TEnum result, out string validationErrorMessage)
        {
            // convert for Blazor component
            if (BindConverter.TryConvertTo(value, CultureInfo.CurrentCulture, out TEnum parsedValue))
            {
                result = parsedValue;
                validationErrorMessage = null;
                return(true);
            }

            // nullable mapping
            if (string.IsNullOrEmpty(value))
            {
                var nullableType = Nullable.GetUnderlyingType(typeof(TEnum));
                if (nullableType != null)
                {
                    result = default;
                    validationErrorMessage = null;
                    return(true);
                }
            }

            // error
            result = default;
            validationErrorMessage = $"The {FieldIdentifier.FieldName} field is not valid.";
            return(false);
        }
示例#11
0
        /// <inheritdoc />
        protected override bool TryParseValueFromString(string value, out TValue result, out string validationErrorMessage)
        {
            if (typeof(TValue) == typeof(string))
            {
                result = (TValue)(object)value;
                validationErrorMessage = null;
                return(true);
            }
            else if (typeof(TValue).IsEnum || (_nullableUnderlyingType != null && _nullableUnderlyingType.IsEnum))
            {
                var success = BindConverter.TryConvertTo <TValue>(value, CultureInfo.CurrentCulture, out var parsedValue);
                if (success)
                {
                    result = parsedValue;
                    validationErrorMessage = null;
                    return(true);
                }
                else
                {
                    result = default;
                    validationErrorMessage = $"The {FieldIdentifier.FieldName} field is not valid.";
                    return(false);
                }
            }

            throw new InvalidOperationException($"{GetType()} does not support the type '{typeof(TValue)}'.");
        }
示例#12
0
        protected override bool TryParseValueFromString(string?value, [MaybeNullWhen(false)] out TEnum result, [NotNullWhen(false)] out string?validationErrorMessage)
        {
            if (BindConverter.TryConvertTo(value, CultureInfo.CurrentCulture, out result))
            {
                validationErrorMessage = null;
                return(true);
            }

            if (string.IsNullOrEmpty(value))
            {
                var nullable = Nullable.GetUnderlyingType(typeof(TEnum));
                if (nullable is not null)
                {
                    result = default;
                    validationErrorMessage = null;
#pragma warning disable CS8762 // Parameter must have a non-null value when exiting in some condition.
                    // Value is nullable in this instance.
                    return(true);

#pragma warning restore CS8762 // Parameter must have a non-null value when exiting in some condition.
                }
            }

            result = default;
            validationErrorMessage = $"Value is not an enum of type {GetValueType().Item1.Name}";
            return(false);
        }
示例#13
0
        protected override bool TryParseValueFromString(string value, out T result, out string validationErrorMessage)
        {
            if (typeof(T) == typeof(string))
            {
                result = (T)(object)value;
                validationErrorMessage = null;
                return(true);
            }

            if (typeof(T).IsEnum || this.NullableUnderlyingType != null && this.NullableUnderlyingType.IsEnum)
            {
                var success = BindConverter.TryConvertTo <T>(value, CultureInfo.CurrentCulture, out var parsedValue);
                if (success)
                {
                    result = parsedValue;
                    validationErrorMessage = null;
                    return(true);
                }
                else
                {
                    throw new InvalidOperationException($"{this.GetType()} does not support the value '{value}'.");
                }
            }

            throw new InvalidOperationException($"{this.GetType()} does not support the type '{typeof(T)}'.");
        }
示例#14
0
        public override void ChangeValue(DateTime value, int index = 0)
        {
            bool result = BindConverter.TryConvertTo <TValue>(
                value.ToString(CultureInfo), CultureInfo, out var dateTime);

            if (result)
            {
                Value[index] = dateTime;
            }

            _pickerStatus[index]._hadSelectValue = true;

            UpdateCurrentValueAsString(index);

            if (IsRange && !IsShowTime && Picker != DatePickerType.Time)
            {
                if (_pickerStatus[0]._hadSelectValue && _pickerStatus[1]._hadSelectValue)
                {
                    Close();
                }
            }
            else if (!IsShowTime && Picker != DatePickerType.Time)
            {
                Close();
            }
        }
        protected void OnInput(ChangeEventArgs args, int index = 0)
        {
            if (args == null)
            {
                return;
            }

            var array = Value as Array;

            if (BindConverter.TryConvertTo(args.Value.ToString(), CultureInfo, out DateTime changeValue))
            {
                if (Picker == DatePickerType.Date)
                {
                    if (IsDateStringFullDate(args.Value.ToString()))
                    {
                        array.SetValue(changeValue, index);
                    }
                }
                else
                {
                    array.SetValue(changeValue, index);
                }
                ChangePickerValue(changeValue, index);

                StateHasChanged();
            }

            UpdateCurrentValueAsString();
        }
示例#16
0
 public static bool TryParseSelectableValueFromString <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TValue>(
     this InputBase <TValue> input, string?value,
     [MaybeNullWhen(false)] out TValue result,
     [NotNullWhen(false)] out string?validationErrorMessage)
 {
     try
     {
         if (BindConverter.TryConvertTo <TValue>(value, CultureInfo.CurrentCulture, out var parsedValue))
         {
             result = parsedValue;
             validationErrorMessage = null;
             return(true);
         }
         else
         {
             result = default;
             validationErrorMessage = $"The {input.DisplayName ?? input.FieldIdentifier.FieldName} field is not valid.";
             return(false);
         }
     }
     catch (InvalidOperationException ex)
     {
         throw new InvalidOperationException($"{input.GetType()} does not support the type '{typeof(TValue)}'.", ex);
     }
 }
示例#17
0
        public static bool SetRealValue <TValue>(this string str, out TValue value)
        {
            value = default(TValue);

            var otype = typeof(TValue);
            var type  = Nullable.GetUnderlyingType(otype) ?? otype;

            if (string.IsNullOrEmpty(str) && otype != typeof(string))
            {
                if (otype != type)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            if (type == typeof(bool))
            {
                if (str.Equals("true", StringComparison.OrdinalIgnoreCase))
                {
                    value = (TValue)(object)true;
                    return(true);
                }
                else if (str.Equals("false", StringComparison.OrdinalIgnoreCase))
                {
                    value = (TValue)(object)false;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            //好奇怪,TryConvertTo<DateTimeOffset> 不行,估计是个bug
            if (type == typeof(DateTimeOffset))
            {
                if (BindConverter.TryConvertToDateTimeOffset(str, CultureInfo.InvariantCulture, out DateTimeOffset val))
                {
                    value = (TValue)(object)val;
                    return(true);
                }

                return(false);
            }
            else
            {
                if (BindConverter.TryConvertTo <TValue>(str, CultureInfo.InvariantCulture, out TValue val))
                {
                    value = val;
                    return(true);
                }

                return(false);
            }
        }
示例#18
0
    public void TryConvertTo_NullableGuid_ValidEmptyOrNull(string incomingValue)
    {
        // Act
        var successfullyConverted = BindConverter.TryConvertTo <Guid?>(incomingValue, CultureInfo.CurrentCulture, out var actual);

        // Assert
        Assert.True(successfullyConverted);
        Assert.Null(actual);
    }
示例#19
0
    public void TryConvertTo_Guid_Invalid(string incomingValue)
    {
        // Act
        var successfullyConverted = BindConverter.TryConvertTo <Guid>(incomingValue, CultureInfo.CurrentCulture, out var actual);

        // Assert
        Assert.False(successfullyConverted);
        Assert.Equal(Guid.Empty, actual);
    }
示例#20
0
        public static T ConvertTo <T>(object obj, T defaultValue)
        {
            if (BindConverter.TryConvertTo <T>(obj, CultureInfo.CurrentCulture, out var convertedValue))
            {
                return(convertedValue);
            }

            return(defaultValue);
        }
示例#21
0
        /// <summary>
        /// 尝试转换指定值为字符串。
        /// </summary>
        /// <param name="value">输入控件的值。</param>
        /// <param name="result">转换后的值。</param>
        /// <param name="validationErrorMessage">转换失败时的错误字符串。</param>
        /// <returns>转换成功返回 <c>true</c>;否则返回 <c>false</c>。</returns>
        protected override bool TryParseValueFromString(string value, out TValue result, out string validationErrorMessage)
        {
            if (BindConverter.TryConvertTo(value, CultureInfo.InvariantCulture, out result))
            {
                validationErrorMessage = null;
                return(true);
            }

            validationErrorMessage = string.Format(ParsingErrorMessage, FieldIdentifier.FieldName);
            return(false);
        }
示例#22
0
 /// <summary>
 /// 入力値をバインド項目値の型に変換します。
 /// </summary>
 /// <param name="value">入力値。</param>
 /// <param name="result">返却値。</param>
 /// <returns>
 /// 入力値をバインド項目値の型に変換し、変換に成功した場合は <c>true</c> 。変換に失敗した場合は <c>false</c> を返却します。
 /// </returns>
 protected override bool TryParseValueFromString(string value, out TValue result)
 {
     if (BindConverter.TryConvertTo <TValue>(value, CultureInfo.InvariantCulture, out result) == true)
     {
     }
     else
     {
         result = default;
     }
     return(true);
 }
示例#23
0
 protected override void OnParametersSet()
 {
     if (FormatValueAsString(ComputedValue) != currentValueAsString)
     //if (!ComputedValue.Equals(currentValue))
     {
         if (BindConverter.TryConvertTo <TValue>(FormatValueAsString(ComputedValue), CultureInfo.CurrentCulture, out var result))
         {
             currentValue = result;
             ValueChanged.InvokeAsync(result);
         }
     }
 }
示例#24
0
    public void TryConvertTo_NullableGuid__Invalid()
    {
        // Arrange
        var value = "invalidguid";

        // Act
        var successfullyConverted = BindConverter.TryConvertTo <Guid?>(value, CultureInfo.CurrentCulture, out var actual);

        // Assert
        Assert.False(successfullyConverted);
        Assert.Null(actual);
    }
示例#25
0
 /// <inheritdoc />
 protected override bool TryParseValueFromString(string?value, [MaybeNullWhen(false)] out TValue result, [NotNullWhen(false)] out string?validationErrorMessage)
 {
     if (BindConverter.TryConvertTo <TValue>(value, CultureInfo.InvariantCulture, out result))
     {
         validationErrorMessage = null;
         return(true);
     }
     else
     {
         validationErrorMessage = string.Format(CultureInfo.InvariantCulture, ParsingErrorMessage, DisplayName ?? FieldIdentifier.FieldName);
         return(false);
     }
 }
示例#26
0
        protected override bool TryParseValueFromString(string value, out TEnum result, out string validationErrorMessage)
        {
            if (BindConverter.TryConvertTo(value, CultureInfo.CurrentCulture, out TEnum parsedValue))
            {
                result = parsedValue;
                validationErrorMessage = null;
                return(true);
            }

            result = default;
            validationErrorMessage = $"The {FieldIdentifier.FieldName} field is not valid.";
            return(false);
        }
示例#27
0
    public void TryConvertTo_NullableGuid_Valid()
    {
        // Arrange
        var expected      = Guid.NewGuid();
        var incomingValue = expected.ToString();

        // Act
        var successfullyConverted = BindConverter.TryConvertTo <Guid?>(incomingValue, CultureInfo.CurrentCulture, out var actual);

        // Assert
        Assert.True(successfullyConverted);
        Assert.Equal(expected, actual.Value);
    }
示例#28
0
        protected override bool TryParseValueFromString(string value, [System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TItem result, [System.Diagnostics.CodeAnalysis.NotNullWhen(false)] out string validationErrorMessage)
        {
            Console.WriteLine($"TryParseValueFromString {value}");
            // Let's Blazor convert the value for us
            if (BindConverter.TryConvertTo(value, System.Globalization.CultureInfo.CurrentCulture, out TItem parsedValue))
            {
                result = parsedValue;
                validationErrorMessage = null;
                return(true);
            }

            result = default;
            validationErrorMessage = $"The {FieldIdentifier.FieldName} field is not valid.";
            return(false);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="value"></param>
        /// <param name="result"></param>
        /// <param name="validationErrorMessage"></param>
        /// <returns></returns>
        protected override bool TryParseValueFromString(string?value, [MaybeNullWhen(false)] out TValue result, [NotNullWhen(false)] out string?validationErrorMessage)
        {
            var ret = false;

            validationErrorMessage = null;
            if (BindConverter.TryConvertTo <TValue>(value, CultureInfo.InvariantCulture, out result))
            {
                ret = true;
            }
            else
            {
                validationErrorMessage = string.Format(CultureInfo.InvariantCulture, ParsingErrorMessage !, DisplayText);
            }
            return(ret);
        }
示例#30
0
        /// <inheritdoc/>
        protected override bool TryParseValueFromString(string value, out T result, out string validationErrorMessage)
        {
            var success = BindConverter.TryConvertTo <T>(value, CultureInfo.CurrentCulture, out var parsedValue);

            if (success)
            {
                result = parsedValue;
                validationErrorMessage = null;
                return(true);
            }
            else
            {
                result = default;
                validationErrorMessage = $"{this.FieldIdentifier.FieldName} field isn't valid.";
                return(false);
            }
        }