Am using MAUI application on .Net 6. Created a custom control for activity indicator, and created bindable property for Running & title.
CODE BEHIND
``public partial class CustomProgressDialog : VerticalStackLayout
{
#region CTOR
public CustomProgressDialog()
{
InitializeComponent();
}
#endregion
#region BINDABLE PROPERTY`
public 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 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
}
<VerticalStackLayout xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Name="this" x:Class="CustomControls.Controls.CustomProgressDialog">
<ActivityIndicator x:Name="Indicator" Grid.Row="1" IsRunning="{Binding Source={x:Reference this}, Path=Running}" ZIndex="9999" WidthRequest="50" Color="#d12b8a" />
<Label x:Name="TitleLabel" Text="{Binding Source={x:Reference this}, Path=Title}" VerticalOptions="Center" HorizontalOptions="Center" />
</VerticalStackLayout>
<custom:CustomProgressDialog Grid.Row="1" Running="{Binding IsBusy}" Title="{Binding Message}" ZIndex="9999"/>
When a giving direct value instead of binding, then there is no error.
<custom:CustomProgressDialog Grid.Row="1" Running="True" Title="Please wait..." ZIndex="9999"/>
But when am using Binding then error.
XFC0009 No property, BindableProperty, or event found for "Title", or mismatching type between value and property.
Please help...
CodePudding user response:
A bindable property can be created by declaring a public static readonly property of type BindableProperty. See Create a bindable property
you could change
public BindableProperty TitleProperty
to
public static readonly BindableProperty TitleProperty
Hope it works for you.