gpt4 book ai didi

c# - 行为未触发 OnPropertyChanged

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:13 25 4
gpt4 key购买 nike

我有一个检查电子邮件地址的自定义行为

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Xamarin.Forms;

namespace AllTheSame.Behaviors.Validator
{
public class EmailValidatorBehavior : Behavior<Entry>
{
private static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(EmailValidatorBehavior), default(bool), BindingMode.TwoWay);

public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;

public bool IsValid
{
get => (bool) GetValue(IsValidProperty);
private set => SetValue(IsValidPropertyKey, value);
}

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

private void HandleTextChanged(object sender, TextChangedEventArgs e)
{
try
{
IsValid = Regex.IsMatch(e.NewTextValue, "^(?(\")(\".+?(?<!\\\\)\"@)|(([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]))$");
}
catch
{
IsValid = false;
}
finally
{
((Entry) sender).TextColor = IsValid ? Color.Default : Color.Red;
}
}

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

我在我的 XAML 页面中这样使用它

...
<ContentPage.BindingContext>
<viewModels:RegisterViewModel />
</ContentPage.BindingContext>
...
<Entry Placeholder="Email" Text="{Binding Email}">
<Entry.Behaviors>
<validator:EmailValidatorBehavior x:Name="EmailValidatorBehavior" IsValid="{Binding IsEmailValid}"/>
</Entry.Behaviors>
</Entry>

我的页面绑定(bind)到我的 RegisterViewModel,并且我在其中有一个属性“IsEmailValid”。

public bool IsEmailValid
{
get => _isEmailValid;
set
{
_isEmailValid = value;
OnPropertyChanged();
SignUpCommand.ChangeCanExecute();
}
}

private bool _isEmailValid;

问题是,当我更改条目的文本时,执行行为中的“HandleTextChanged”方法,但我的“RegisterViewModel”中的属性“IsEmailValid”的值没有改变。我可能在这里做错了什么,但我想不通。我尝试在我的行为中使用 BindableProperty.Create,但没有任何改变。

你能帮帮我吗?

最佳答案

难道您需要一个 INotifyPropertyChanged?这是一个例子:

public class Data : INotifyPropertyChanged
{
// boiler-plate
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, string propertyName)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}

// props
private string name;
public string Name
{
get { return name; }
set { SetField(ref name, value, "Name"); }
}

关于c# - 行为未触发 OnPropertyChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43908996/

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