【问题标题】:Xamarin Community Toolkit input validation is true when BindingContext is set设置 BindingContext 时,Xamarin 社区工具包输入验证为真
【发布时间】:2022-01-21 02:10:20
【问题描述】:

设置 BindingContext 时,即使所需参数不正确,IsValid 也会将 IsLastnameValid 和 IsFirstnameValid 设置为 true。我不明白为什么。

XMAL 代码

<StackLayout>
    <Entry Placeholder="Lastname" Text="{Binding Lastname}">
        <Entry.Behaviors>
            <xct:TextValidationBehavior MinimumLength="3" MaximumLength="10"
                IsValid="{Binding IsLastnameValid}"/>
        </Entry.Behaviors>
    </Entry>

    <Entry Placeholder="Firstname" Text="{Binding Firstname}">
        <Entry.Behaviors>
            <xct:TextValidationBehavior MinimumLength="3" MaximumLength="10"
                IsValid="{Binding IsFirstnameValid}"/>
        </Entry.Behaviors>
    </Entry>

    <Button Text="Save" Clicked="OnSave">
        <Button.IsEnabled>
            <MultiBinding  Converter="{StaticResource BooleanAndConverter}">
                <Binding Path="IsLastnameValid"/>
                <Binding Path="IsFirstnameValid"/>
            </MultiBinding>
        </Button.IsEnabled>
    </Button>
</StackLayout>

CS 代码

public partial class MainPage : ContentPage
{
    private UserViewModel _userViewModel;

    public MainPage()
    {
        InitializeComponent();
        _userViewModel = new UserViewModel(false, false);
        BindingContext = _userViewModel;
    }

    private void OnSave(object sender, EventArgs e)
    {
        Console.WriteLine("[User View Model Firstname] : " + _userViewModel.Firstname + "  " + _userViewModel.IsFirstnameValid);
        Console.WriteLine("[User View Model Lastname] : " + _userViewModel.Lastname + "  " + _userViewModel.IsLastnameValid);
    }
}

ViewModel 代码 我正在使用 Nuget 包 PropertyChanged.Fody

[AddINotifyPropertyChangedInterface]
public class UserViewModel
{
    public UserViewModel(bool isLastnameValid, bool isFirstnameValid)
    {
        IsLastnameValid = isLastnameValid;
        IsFirstnameValid = isFirstnameValid;
    }

    public string Lastname { get; set; }
    public string Firstname { get; set; }
    public bool IsLastnameValid { get; set; }
    public bool IsFirstnameValid { get; set; }
}

感谢您的帮助:)

已编辑以显示转换器代码

public class BooleanAndConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {

        if (values.Any(value => value == null))
        {
            return false;
        }

        var result = values.OfType<IConvertible>().All(System.Convert.ToBoolean);
        return result;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException("BooleanAndConverter is a OneWay converter.");
    }
}

【问题讨论】:

  • 这能回答你的问题吗? Xamarin Community Toolkit input validation is true even when Entry is empty&lt;xct:TextValidationBehavior ... Flags="ValidateOnAttaching" ...&gt;
  • @ToolmakerSteve 我已经尝试过了,没有任何区别 :( 我也尝试了不同的标志没有区别,或者我以错误的方式使用它们
  • 嗯。我将您的代码复制到我的测试应用程序中,对其进行了测试,并且设置该标志确实会改变行为,对我来说。 (尽管我在没有 Fody 的情况下完成了这项工作——我会在短时间内进行测试。)您是在 Android 还是 iOS 上进行测试?模拟器还是特定的设备型号?你可以download ToolmakeSteve - TestXFBugs。其中,项目TestBugsApp.xaml.cs 具有MainPage = new TextValidationPage(); 行-取消注释该行,并注释掉任何其他“MainPage =”行。看看这是否适合你。
  • 直接在Android手机上,Hawaii pro 20 mate
  • 如果你在安卓模拟器上运行,是否也会出现这种情况?

标签: c# xamarin xamarin.forms


【解决方案1】:

此代码在我的测试存储库中有效(请参阅我上面评论中的链接):

<Entry Placeholder="Lastname" Text="{Binding Lastname}">
    <Entry.Behaviors>
        <xct:TextValidationBehavior MinimumLength="3" MaximumLength="10"
         Flags="ValidateOnAttaching, ValidateOnValueChanging" IsValid="{Binding IsLastnameValid}"/>
    </Entry.Behaviors>
</Entry>

与您的唯一变化是添加了属性

Flags="ValidateOnAttaching, ValidateOnValueChanging"

鉴于此,按下“保存”按钮会导致调试输出为 IsValid 的“false”。


已编辑:如果您手动设置标志,则默认标志不适用。所以需要列出所有需要的标志。我把上面的代码sn-p改了。

ValidationFlags enum

您可能还想添加, ValidateOnUnfocusing。当用户离开该控件(例如单击另一个控件)时,这会再次检查。

【讨论】:

  • 是的,此解决方案有效,但现在该按钮始终处于禁用状态,因为从未触发“转换器”。
  • 让我编辑帖子向您展示转换器
  • 我明白你的意思 - 开始后不调用转换器。有趣的事实(我刚刚学到的) - 见 EDITTED。
  • 是的!你找到了!!非常感谢
猜你喜欢
  • 2021-05-12
  • 2021-10-12
  • 2021-09-21
  • 2021-10-08
  • 2021-05-06
  • 2021-08-04
  • 1970-01-01
  • 2021-07-25
  • 2013-02-09
相关资源
最近更新 更多