C# 如何在按钮上绑定IsValidProperty行为

C# 如何在按钮上绑定IsValidProperty行为,c#,xamarin.forms,C#,Xamarin.forms,我正在尝试实现一个验证控件 我有一个页面,其中有一个条目和一个ok按钮 该条目是一个电子邮件输入,我实现了一个行为,向用户显示他的输入是否正确。我想将按钮的IsEnabled属性绑定到行为的IsValid属性 以下是行为代码: public class EmailValidatorBehavior : Behavior<Entry> { const string emailRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.

我正在尝试实现一个验证控件

我有一个页面,其中有一个条目和一个ok按钮

该条目是一个电子邮件输入,我实现了一个行为,向用户显示他的输入是否正确。我想将按钮的IsEnabled属性绑定到行为的IsValid属性

以下是行为代码:

public class EmailValidatorBehavior : Behavior<Entry>
{
    const string emailRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
        @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$";


    public static BindableProperty IsValidProperty = BindableProperty.Create("IsValid", typeof(bool), typeof(EmailValidatorBehavior), false);

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

    protected override void OnAttachedTo(Entry entry)
    {
        entry.TextChanged += HandleTextChanged;
        base.OnAttachedTo(entry);
    }

    void HandleTextChanged(object sender, TextChangedEventArgs e)
    {
        bool IsValid = false;
        IsValid = (Regex.IsMatch(e.NewTextValue, emailRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)));
        ((Entry)sender).TextColor = IsValid ? Color.Default : Color.Red;
    }

    protected override void OnDetachingFrom(Entry entry)
    {
        entry.TextChanged -= HandleTextChanged;
        base.OnDetachingFrom(entry);
    }
}
另外,我的页面有一个bindingContext,它是一个视图模型。正如我所知,我将在这里用我的逻辑来绑定。我看到我必须监听IsValidProperty上的属性更改事件,但我无法解决如何实现它。尽管我在xamarin的网站上读了很多文档,但对于binding和bindableProperty我还是不太清楚,特别是在行为和创建bindableProperty方面。我已经开始:

第页:

     var behavior = new EmailValidatorBehavior();
        //Binding context is LandingViewModel 
        behavior.SetBinding(EmailValidatorBehavior.IsValidProperty, "IsValid");
        _okButton.SetBinding(MR.Gestures.Button.IsEnabledProperty, "IsEnabled");
        _emailEntry.Behaviors.Add(behavior);
这是视图模型:

public class LandingViewModel : BaseViewModel
{

    private bool _isEnabled;
    private bool _isValid;

    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            if (_isEnabled == value) return;
            _isEnabled = value;
            OnPropertyChanged("IsEnabled");
        }   
    }

    public bool IsValid
    {
        get { return _isValid; }
        set
        {
            Debug.WriteLine(_isValid);
            if (_isValid == value) return;
            _isValid = value;
            IsEnabled = _isValid;
            Debug.WriteLine(_isValid);
            OnPropertyChanged("IsValid");
        }
    }
我不高兴,我看到我明显错过了什么,但看不到什么


多谢各位

您的视图模型中没有
IsEnabled
属性,因此行
OnPropertyChanged(“IsEnabled”)根本不会做任何事情。我认为您需要做的是将
SetIsEnabledButton
属性的名称更改为
IsEnabled
,并更改行
\u IsEnabled=\u isValid
to
IsEnabled=\u是有效的。这样,当设置了
IsValid
时,它会将控制权转移到
IsEnabled
设置程序,然后该设置程序会为自己发出属性更改通知


如果您仍然无法使其正常工作,请返回此处。

您的视图模型中没有
IsEnabled
属性,因此行
OnPropertyChanged(“IsEnabled”)根本不会做任何事情。我认为您需要做的是将
SetIsEnabledButton
属性的名称更改为
IsEnabled
,并更改行
\u IsEnabled=\u isValid
to
IsEnabled=\u是有效的。这样,当设置了
IsValid
时,它会将控制权转移到
IsEnabled
设置程序,然后该设置程序会为自己发出属性更改通知


如果仍然无法使其正常工作,请返回此处。

查看您添加的
Debug.WriteLine()
,我猜
是有效的
属性不是由绑定设置的

这是因为您没有设置
BindableProperty
DefaultBindingMode
,或者因为您没有设置
BindingMode
本身的
BindingMode
。因此绑定默认为单向
,这意味着当视图模型更改时,您的行为
是有效的
,而不是相反的方式

通过以下方式创建您的
BindableProperty

公共静态BindableProperty是ValidProperty=
创建(“IsValid”、typeof(bool)、typeof(EmailValidatorBehavior),
false,BindingMode.TwoWay);
这应该行得通

请允许我补充几点:

1.不需要遍历ViewModel 您可以将
条目
IsEnabled
属性直接绑定到行为
IsValid
属性

var-behavior=new-EmailValidatorBehavior();
_SetBinding(MR.signatures.Button.IsEnabledProperty,新绑定(“IsEnabled”,来源:behavior));
_emailEntry.Behaviors.Add(行为);
如果这样做,行为将成为绑定的源,您不必创建
BindableProperty
,只需实现
INotifyPropertyChanged

2.确保不要在
Entry
实例之间共享行为
如果在样式中共享此行为,则将重用同一实例,并且它将无法按预期工作。

查看您添加的
Debug.WriteLine()
,我猜
IsValid
属性不是由绑定设置的

这是因为您没有设置
BindableProperty
DefaultBindingMode
,或者因为您没有设置
BindingMode
本身的
BindingMode
。因此绑定默认为单向
,这意味着当视图模型更改时,您的行为是有效的,而不是相反的方式

通过以下方式创建您的
BindableProperty

公共静态BindableProperty是ValidProperty=
创建(“IsValid”、typeof(bool)、typeof(EmailValidatorBehavior),
false,BindingMode.TwoWay);
这应该行得通

请允许我补充几点:

1.不需要遍历ViewModel 您可以将
条目
IsEnabled
属性直接绑定到行为
IsValid
属性

var-behavior=new-EmailValidatorBehavior();
_SetBinding(MR.signatures.Button.IsEnabledProperty,新绑定(“IsEnabled”,来源:behavior));
_emailEntry.Behaviors.Add(行为);
如果这样做,行为将成为绑定的源,您不必创建
BindableProperty
,只需实现
INotifyPropertyChanged

2.确保不要在
Entry
实例之间共享行为
如果您在样式中共享此行为,则同一实例将被重用,并且它将无法按预期工作。

我已经找到了使其工作的方法,并且由于@Stephane Delcroix post,我不需要任何viewmodel来实现这一点

按照@Stephane Delcroix的建议,我仍然拥有关于行为的可绑定属性

public static BindableProperty IsValidProperty = BindableProperty.Create("IsValid", typeof(bool), typeof(EmailValidatorBehavior), false, BindingMode.TwoWay);
要在isValid属性上绑定我的按钮,我编写了:

_okButton.SetBinding(IsEnabledProperty,
                new Binding(EmailValidatorBehavior.IsValidProperty.PropertyName));
“引用PropertyName而不是像“IsValid”这样的“Value”是一种很好的做法,这样可以避免字符串中的任何打字错误,编译器和补全程序都可以避免错误”

我设置了m的bindingContext
_okButton.SetBinding(IsEnabledProperty,
                new Binding(EmailValidatorBehavior.IsValidProperty.PropertyName));
 _okButton.BindingContext = _emailBehavior;