【问题标题】:xamarin.forms entry binding on blur模糊上的 xamarin.forms 条目绑定
【发布时间】:2021-09-30 21:06:22
【问题描述】:

条目文本的默认绑定触发器似乎是 TextChanged 事件。我想推迟更新源,直到模糊事件。在 WPF 中有可以设置为修改绑定触发器的 UpdateSourceTrigger 参数,但我在 Xamarin.Form 中没有找到任何文档。

如何在 Xaramin.Forms 中通过 XAML 中的绑定来实现。由于显而易见的原因,我不想在后面的代码中手动处理它。

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    这个问题很老,但仍然很实际。特别是对于带小数点的字段... 使用 Xamarin 社区工具包EventToCommandBehavior(或类似实现,如 Prism)的第一个选项。然后可以将“Unfocused”事件绑定到某个命令:

        <Entry.Behaviors>
        <xct:EventToCommandBehavior
            EventName="Unfocused"
            Command="{Binding MyCustomCommand}" />
        </Entry.Behaviors>
    

    添加处理此事件的自定义 Entry 组件的其他选项,此选项更适合我使用带小数点的数字输入(浮点、双精度、小数)。这是来自 MSDN 论坛Entry binding Decimal 的稍微修改的解决方案:

    public class NumericEntry : Entry
        {
            #region Bindables
    
            public static readonly BindableProperty NumericValueProperty = BindableProperty.Create(
                "NumericValue",
                typeof(decimal?),
                typeof(NumericEntry),
                null,
                BindingMode.TwoWay,
                coerceValue: (_, value) => (decimal?)value,
                propertyChanged: (bindable, _, __) => SetDisplayFormat((NumericEntry)bindable)
            );
    
            public static readonly BindableProperty NumericValueFormatProperty = BindableProperty.Create(
                "NumericValueFormat",
                typeof(string),
                typeof(NumericEntry),
                "N0",
                BindingMode.TwoWay,
                propertyChanged: (bindable, _, __) => SetDisplayFormat((NumericEntry)bindable)
            );
    
            #endregion Bindables
    
            #region Constructor
    
            public NumericEntry()
            {
                Keyboard = Keyboard.Numeric;
                Focused += OnFocused;
                Unfocused += OnUnfocused;
            }
    
            #endregion Constructor
    
            #region Events
    
            private void OnFocused(object sender, FocusEventArgs e)
            {
                SetEditFormat(this);
            }
    
            private void OnUnfocused(object sender, FocusEventArgs e)
            {
                var numberFormant = CultureInfo.CurrentCulture.NumberFormat;
                var _text = Text.Replace(".", numberFormant.NumberDecimalSeparator);
    
                if (decimal.TryParse(_text, NumberStyles.Number, CultureInfo.CurrentCulture, out var numericValue))
                {
                    var round = Convert.ToInt32(NumericValueFormat.Substring(1));
                    NumericValue = Math.Round(numericValue, round);
                }
                else
                {
                    NumericValue = null;
                }
    
                SetDisplayFormat(this);
            }
    
            #endregion Events
    
            #region Properties
    
            public decimal? NumericValue
            {
                get => (decimal?)GetValue(NumericValueProperty);
                set => SetValue(NumericValueProperty, value);
            }
    
            public string NumericValueFormat
            {
                get => (string)GetValue(NumericValueFormatProperty) ?? "N0";
                set
                {
                    var _value = string.IsNullOrWhiteSpace(value) ? "N0" : value;
                    SetValue(NumericValueFormatProperty, _value);
                }
            }
    
            #endregion Properties
    
            #region Methods
    
            private static void SetDisplayFormat(NumericEntry textBox)
            {
                if (textBox.NumericValue.HasValue)
                {
                    textBox.Text = textBox.NumericValue.Value.ToString(textBox.NumericValueFormat, CultureInfo.DefaultThreadCurrentCulture);
                }
                else
                {
                    textBox.Text = string.Empty;
                }
            }
    
            private static void SetEditFormat(NumericEntry textBox)
            {
                if (textBox.NumericValue.HasValue)
                {
                    var numberFormant = CultureInfo.CurrentCulture.NumberFormat;
                    textBox.Text = textBox.NumericValue.Value.ToString(textBox.NumericValueFormat, CultureInfo.CurrentCulture).Replace(numberFormant.NumberGroupSeparator, string.Empty);
                }
                else
                {
                    textBox.Text = string.Empty;
                }
            }
    
            #endregion Methods
        }
    

    并像这样使用它:

           // import our component
           xmlns:ex="clr-namespace:BoganPos.Extensions"
           //...
           <ex:NumericEntry NumericValue="{Binding DecimalValue}" NumericValueFormat="F2" Placeholder="Placeholder"/>
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多