MAUI自定义控件绑定报错XFC0009:找不到LeftSetsValue属性
MAUI自定义控件绑定错误XFC0009解决方法
核心问题:BindableProperty命名不符合规范
你的自定义控件中,可绑定属性的命名违反了MAUI的约定:属性对应的BindableProperty必须命名为「属性名+Property」。当前属性是LeftSetsValue,但BindableProperty命名为LeftSetsProperty,缺少Value后缀,导致XAML解析时无法匹配到对应的可绑定属性,触发XFC0009错误。
修正代码
将自定义控件中的BindableProperty定义修改为:
public static readonly BindableProperty LeftSetsValueProperty = BindableProperty.Create( nameof(LeftSetsValue), typeof(string), typeof(ExerciseSelection), string.Empty, defaultBindingMode: BindingMode.TwoWay); public string LeftSetsValue { get => (string)GetValue(LeftSetsValueProperty); set => SetValue(LeftSetsValueProperty, value); }
额外检查项
如果修正后仍报错,排查以下几点:
- 自定义控件类访问权限:确保
ExerciseSelection类是public修饰的,否则ContentPage无法正确识别该控件的属性。 - XAML命名空间引用:确认ContentPage的XAML头部已正确引用自定义控件所在的命名空间,示例:
替换xmlns:controls="clr-namespace:YourAppName.Controls"YourAppName.Controls为实际的命名空间路径。 - ViewModel属性拼写:你绑定的
SharedVM.LeftSetForExptyCan中Expty是Empty的笔误,虽不会导致编译错误,但运行时可能出现绑定失效,建议修正为LeftSetForEmptyCan并同步ViewModel中的属性名。
内容的提问来源于stack exchange,提问作者RMR
相关产品推荐
相关产品推荐

