gpt4 book ai didi

xamarin.forms 条目绑定(bind)模糊

转载 作者:行者123 更新时间:2023-12-04 14:00:39 25 4
gpt4 key购买 nike

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

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

最佳答案

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

    <Entry.Behaviors>
<xct:EventToCommandBehavior
EventName="Unfocused"
Command="{Binding MyCustomCommand}" />
</Entry.Behaviors>
添加处理此事件的自定义 Entry 组件的其他选项,此选项更适合带小数点的数字输入(浮点数、 double 数、小数点)。这是来自 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"/>

关于xamarin.forms 条目绑定(bind)模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48253615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com