无法绑定到自定义行为的BindableProperty,-Xamarin forms/Prism

2j4z5cfb  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(184)

我有一个自定义的行为。它有一个可绑定的属性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的所有其他字符串属性,但仍然没有运气。

gjmwrych

gjmwrych1#

XFC0009:找不到“CodeInString”的属性、BindableProperty或事件,或者值和属性之间的类型不匹配。
这个错误表示“CodeInString”属性找不到,虽然我知道你可能认为你已经在OnAttachedTo方法中设置了:

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

但是这一行是在MainPage将BindingContext设置为页面的viewmodel之前执行的,所以MobileNumberValidation的bindingContext为null,所以我们找不到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;
    ....
}

希望对你有用。

相关问题