不能绑定到自定义行为的BindableProperty.xamarin的形式/棱镜



我为条目设置了一个自定义行为。它有一个可绑定的属性MobileCountryCode。

public class MobileNumberValidation:Behavior<Entry>
{
public static readonly BindableProperty MobileCountryCodeProperty =
BindableProperty.Create(nameof(MobileCountryCode)
,typeof(string),typeof(MobileNumberValidation)
,defaultValue:String.Empty);
public string MobileCountryCode
{
get=> (string)GetValue(MobileCountryCodeProperty);
set => SetValue(MobileCountryCodeProperty, value);
}

protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
this.BindingContext = bindable.BindingContext;
bindable.Unfocused += Bindable_Unfocused;
}
protected override void OnDetachingFrom(Entry bindable)
{
base.OnDetachingFrom(bindable);
bindable.Unfocused -= Bindable_Unfocused;
}

private void Bindable_Unfocused(object sender, FocusEventArgs e)
{
// Validation logic
}
}

我在xaml中使用了这种自定义行为,并将硬编码的字符串值赋给属性'MobileCountryCode',它工作得很好。

<Entry
x:Name="ent_MobileNo"
Grid.Column="1"
behaviors:SetFocusOnEntryCompletedBehavior.NextElement="{x:Reference ent_name}"
Keyboard="Email"
Placeholder="{behaviors:Translate MobilePlaceHolder}"
Text="">
<Entry.Behaviors>
<customBehaviour:MobileNumberValidation
x:Name="MobileNoFormat"
InValidStyle="{x:StaticResource InvalidEntry}"
MobileCountryCode="+1" />
</Entry.Behaviors>
</Entry>
<!--this is all good-->

但是当我将硬编码字符串的值更改为Binding时,它会给出一个错误XFC0009: No property, BindableProperty, or event found for "CodeInString", or mismatching type between value and property.

<Entry
x:Name="ent_MobileNo"
Grid.Column="1"
behaviors:SetFocusOnEntryCompletedBehavior.NextElement="{x:Reference ent_name}"
Keyboard="Email"
Placeholder="{behaviors:Translate MobilePlaceHolder}"
Text="">
<Entry.Behaviors>
<customBehaviour:MobileNumberValidation
x:Name="MobileNoFormat"
InValidStyle="{x:StaticResource InvalidEntry}"
MobileCountryCode="{Binding CodeInString}" />
</Entry.Behaviors>
</Entry>
<!--this gives error-->

CodeInString是页面视图模型中的一个属性。

public string CodeInString {get; set;} = "+22"

如何解决这个问题??

我在这里遇到过其他类似的问题,但没有一个解决方案对我有效。相同的值可以绑定到标签文本和条目文本,它运行没有任何问题,并给出正确的值。我已经将它绑定到VM的所有其他字符串属性,但仍然没有运气。

编辑:我添加了一个bool类型的额外可绑定属性IsInValid。这在绑定到viewmodel属性时也有同样的问题。

public class MobileNumberValidation:Behavior<Entry>
{
private static readonly BindableProperty IsInValidProperty = 
BindableProperty.Create(nameof(IsInValid),typeof(bool),typeof(MobileNumberValidation),defaultValue:false);
public bool IsInValid
{
get=>(bool)GetValue(IsInValidProperty);
set => SetValue(IsInValidProperty, value);
}
public static readonly BindableProperty MobileCountryCodeProperty =
BindableProperty.Create(nameof(MobileCountryCode)
,typeof(string),typeof(MobileNumberValidation)
,defaultValue:String.Empty);
public string MobileCountryCode
{
get=> (string)GetValue(MobileCountryCodeProperty);
set => SetValue(MobileCountryCodeProperty, value);
}
protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
bindable.Unfocused += Bindable_Unfocused;
}
protected override void OnDetachingFrom(Entry bindable)
{
base.OnDetachingFrom(bindable);
bindable.Unfocused -= Bindable_Unfocused;
}
private void Bindable_Unfocused(object sender, FocusEventArgs e)
{
// Validation logic
}
}
Xaml部分
<Entry
x:Name="ent_MobileNo"
Grid.Column="1"
Keyboard="Email"
Placeholder="{behaviors:Translate MobilePlaceHolder}"
Text="{Binding MobileNo}">
<Entry.Behaviors>
<customBehaviour:MobileNumberValidation
x:Name="MobileNoFormat"
BindingContext="{Binding Path=BindingContext, Source={Reference thisPage}}"
IsInValid="{Binding IsMobileNoValid}"
MobileCountryCode="{Binding MobileCountryCodeSelected.MobileCode}" />
</Entry.Behaviors>
</Entry>

IsMobileNoValid是页面ViewModel中的布尔属性。

但是当它绑定到时是无效的customBehaviour:MobileNumberValidation它会给出与之前相同的错误,但针对的是IsInValid属性。这适用于硬编码的布尔值(IsInValid="False"这个工作)。MobileCountryCode

当前运行正常,没有错误。

XFC0009:没有找到" codestring"的属性,BindableProperty或事件,或值和属性之间的类型不匹配。

此错误表明" codestring& quot;属性找不到尽管我知道你可能认为你已经在中设置了onattachdto方法:

protected override void OnAttachedTo(Entry bindable)
{
this.BindingContext = bindable.BindingContext;
....
}

然而,这一行是在MainPage将其BindingContext设置为page的视图模型之前执行的。因此,MobileNumberValidation的bindingContext是零。所以我们找不到CodeInString财产。

所以为了解决这个问题,你必须手动设置BindingContext,也许将MobileNumberValidation的BindingContext设置为页面BindingContext:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
...
x:Name="this">
<Entry.Behaviors>
<customBehaviour:MobileNumberValidation BindingContext="{Binding Path=BindingContext,Source={x:Reference this}}"
x:Name="MobileNoFormat"
MobileCountryCode="{Binding CodeInString}" />
</Entry.Behaviors>

别忘了删除OnAttachedTo中的这一行方法:

protected override void OnAttachedTo(Entry bindable)
{
this.BindingContext = bindable.BindingContext;
....
}

希望它对你有用。

最新更新