Xamarin Forms最佳行为和验证

4
我需要知道如何访问我的视图模型中这些行为的IsValid属性。
我希望您告诉我一个更强大的行为,因为它们是从头开始制作的,我想使用一些NuGet包来进行更强大的验证,尽管我对Xamarin Forms还很陌生。
这是我的行为,但我无法访问我的视图模型中的“IsValid”属性:
  public class MesesTrabalhadosValidatorBehavior : Behavior<Entry>
        {
            private static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(MesesTrabalhadosValidatorBehavior), false);

            public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;

            public bool IsValid
            {
                get { return (bool)base.GetValue(IsValidProperty); }
                private set { base.SetValue(IsValidPropertyKey, value); }
            }

            private void bindable_TextChanged(object sender, TextChangedEventArgs e)
            {
                double result;
                double.TryParse(e.NewTextValue, out result);
                IsValid = result > 0;

                ((Entry)sender).TextColor = IsValid ? Color.Green : Color.Red;
            }

            protected override void OnAttachedTo(Entry bindable)
            {
                bindable.TextChanged += bindable_TextChanged;
            }

            protected override void OnDetachingFrom(Entry bindable)
            {
                bindable.TextChanged -= bindable_TextChanged;
            }
        }

我的观点:

    <Entry Placeholder="Mêses trabalhados" Text="{Binding MesesTrabalhados}" Keyboard="Numeric">
          <Entry.Behaviors>
            <local:MesesTrabalhadosValidatorBehavior />
          </Entry.Behaviors>
        </Entry>

        <Entry Placeholder="Último salário" Text="{Binding Salario}" Keyboard="Numeric">
          <Entry.Behaviors>
            <local:SalarioValidatorBehavior />
          </Entry.Behaviors>
        </Entry>

    <ContentView Padding="0,20,0,0">
      <Button Text="Calcular" HorizontalOptions="Fill" IsEnabled="{Binding IsValid}" Command="{Binding CalcularFgtsCommand, Mode=TwoWay}"/>
    </ContentView>

我不确定百分之百,但这更像是一个代码审查的StackExchange问题,而不是其他什么。 - Kevin Avignon
1个回答

3
您需要添加一个参数,我的建议是使用命令函数,这样当它达到某些条件时,您可以调用它。例如:
<local:SalarioValidatorBehavior Command="{Binding MyCommandInViewModel}" />

然后是行为中的可绑定属性。

    public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(TextChangedBehavior), null);

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    void OnEntryTextChanged(object sender, TextChangedEventArgs args)
    {
        if (Command != null)
            Command.Execute(null);
    }

或者您可以在行为中使用IsValid属性,然后将其绑定回ViewModel中的属性。

<local:SalarioValidatorBehavior IsValid="{Binding IsValid, Mode=TwoWay}" />

我的命令必须通过点击按钮来激活,这是为这个命令绑定的。如果我没错的话,答案会自动调用该命令,但这不是我想要的。您知道如何修复吗?一旦您到达工作岗位,您可以更改答案,我会测试这个变化。 - Luiz Negrini
@LuizNegrini - 尝试在IsValid属性上进行双向绑定,如上所示。 - Adam
@AdamPedley 你能否请看一下这个问题:http://stackoverflow.com/questions/43679889/multiple-behaviors-in-entrybindable-property-change-is-not-reflected - b.g

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接