1
我有一個行爲,如果他太短,它負責着色文本。我不僅要使用Entry控件的行爲,還要使用搜索欄或編輯器等其他許多功能。我如何將這種行爲轉化爲泛型?可能嗎?我希望你明白我的意思。如何將行爲轉換爲Xamarin Forms中的泛型類型?
public class MinLengthValidatonBehavior : Behavior<Entry>
{
public static readonly BindableProperty MinLengthProperty =
BindableProperty.Create("MinLength", typeof(int), typeof(MinLengthValidatonBehavior), 0);
public static readonly BindableProperty InvalidColorProperty =
BindableProperty.Create("InvalidColor", typeof(string), typeof(EmailValidatonBehavior), "e4375b");
public int MinLength
{
get => (int)GetValue(MinLengthProperty);
set => SetValue(MinLengthProperty, value);
}
public string InvalidColor
{
get => GetValue(InvalidColorProperty).ToString();
set => SetValue(InvalidColorProperty, value);
}
protected override void OnAttachedTo(Entry bindable)
{
bindable.TextChanged += BindableTextChanged;
}
private void BindableTextChanged(object sender, TextChangedEventArgs e)
{
(sender as Entry).TextColor =
e.NewTextValue.Length < MinLength ? Color.FromHex(InvalidColor) : Color.Default;
}
protected override void OnDetachingFrom(Entry bindable)
{
bindable.TextChanged -= BindableTextChanged;
}
}
對不起,我沒有寫這麼久。我以前嘗試過,但我有錯誤:'類型'T'不能用作通用類型或方法'行爲'中的類型參數'T'。沒有從'T'到'Xamarin.Forms.BindableObject'的裝箱轉換或類型參數轉換。' –
vamteusz
有一個類似的帖子,你是問題[這裏](https://stackoverflow.com/questions/24529746/類型不能待使用-作爲型參數叔在最通用型或-方法-原因)。看看,如果它不夠,嘗試提供更多的數據。 – katxalot