xamarin 找不到“IsChecked”的属性、BindableProperty或事件,或者值和属性之间的类型不匹配

qrjkbowd  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(111)

我创建的控件没有IsChecked属性,所以我创建了它,但当我试图绑定到它时,我收到了此错误。您能告诉我错误在哪里吗?

<Ctrl:CtrlImageToggleButton
                                            x:Name="xEnablePV"
                                            Grid.Row="7"
                                            Grid.Column="1"
                                            HorizontalOptions="Start"
                                            IsChecked="{Binding EnablePV}"
                                            ToggleOffImageSource="btn_off.png"
                                            ToggleOnImageSource="btn_on.png" />


   public class CtrlImageToggleButton : ImageButton
        {
            private bool bCheck = false;
            public delegate void eStatusChanged();
            public event eStatusChanged event_StatusChanged;
                      
            public bool IsChecked 
            {
                get
                {
                    return bCheck;
                }
                set
                {
                    bCheck = value;
                    SetValue(IsCheckedProPerty, value);
    
                    if (this.event_StatusChanged != null)
                    {
                        this.event_StatusChanged();
                    }
                }       
            }
    
    
            public static readonly BindableProperty IsCheckedProPerty = BindableProperty.Create(
                                                              propertyName: "IsCheckedProPerty",
                                                              returnType: typeof(bool),
                                                              declaringType: typeof(CtrlImageToggleButton),
                                                              defaultValue: false,
                                                              defaultBindingMode: BindingMode.TwoWay,
                                                              propertyChanged: IsCheckedProPertyChanged);
    
            private static void IsCheckedProPertyChanged(BindableObject bindable, object oldValue, object newValue)
            {
                (bindable as CtrlImageToggleButton).IsChecked = (bool)newValue;
            }       
        }
    }

XFC0009找不到“IsChecked”的属性、BindableProperty或事件,或值和属性之间的类型不匹配。POP D:\ProgramSrc\POP Renewal\POP\POP\Component\Spectrum\Spectrum_OptionPage.xaml 196

cgh8pdjw

cgh8pdjw1#

对于Bindable属性,有一个固定的命名约定,您必须遵循该约定,因此您的属性名称应该如下所示:

public static readonly BindableProperty IsCheckedProperty = BindableProperty.Create(
                                                          propertyName: nameof(IsChecked),
                                                          returnType: typeof(bool),
                                                          declaringType: typeof(CtrlImageToggleButton),
                                                          defaultValue: false,
                                                          defaultBindingMode: BindingMode.TwoWay,
                                                          propertyChanged: IsCheckedPropertyChanged);

祝你好运!

相关问题