.NET 6 MAUI自定义控件绑定Title时触发XFC0009错误求助
解决MAUI自定义控件绑定XFC0009错误
问题原因
你遇到的XFC0009错误,核心原因是可绑定属性的声明不符合MAUI要求:
- MAUI规定,自定义可绑定属性必须声明为
public static readonly类型的字段 - 当前的
TitleProperty和RunningProperty是普通的public实例字段,XAML编译器无法识别为合法的可绑定属性,因此绑定时报错。直接赋值时调用的是CLR属性(Title/Running),所以不会报错,但数据绑定依赖可绑定属性的静态字段,因此失败。
解决步骤
将两个可绑定属性的声明改为public static readonly,具体修改如下:
修改后的后台代码
public partial class CustomProgressDialog : VerticalStackLayout { #region CTOR public CustomProgressDialog() { InitializeComponent(); } #endregion #region BINDABLE PROPERTY // 修改为public static readonly public static readonly BindableProperty TitleProperty = BindableProperty.Create( propertyName: nameof(Title), returnType: typeof(string), declaringType: typeof(CustomProgressDialog), defaultValue: "Please wait...", defaultBindingMode: BindingMode.TwoWay, propertyChanged: (bindable, oldvalue, newvalue) => { var control = (CustomProgressDialog)bindable; control.TitleLabel.Text = newvalue as string; }); // 修改为public static readonly public static readonly BindableProperty RunningProperty = BindableProperty.Create( propertyName: nameof(Running), returnType: typeof(bool), declaringType: typeof(CustomProgressDialog), defaultValue: false, defaultBindingMode: BindingMode.TwoWay, propertyChanged: (bindable, oldvalue, newvalue) => { var control = (CustomProgressDialog)bindable; control.Indicator.IsRunning = (bool)newvalue; }); #endregion #region PROPERTY public string Title { get => (string)GetValue(TitleProperty); set => SetValue(TitleProperty, value); } public bool Running { get => (bool)GetValue(RunningProperty); set => SetValue(RunningProperty, value); } #endregion }
验证说明
修改后,XAML编译器能够正确识别Title和Running对应的可绑定属性,数据绑定Running="{Binding IsBusy}"和Title="{Binding Message}"即可正常工作,同时直接赋值的方式也不受影响。
内容的提问来源于stack exchange,提问作者abhi
相关产品推荐
相关产品推荐

