cxamarinbindableproperty

How to Apply Behaviors on Xamarin Forms Platform


I want to numeric counterup/down animation my xamarin forms application. I found the code example here

But i haven't applied. How can i apply this behavior class Xamarin Forms label control.

I tried this code but not work.

 <Label x:Name="lblScore"  FontSize="24"  TextColor="Black" Text="{Binding Number}" HorizontalOptions="Center" VerticalOptions="CenterAndExpand">
                        <Label.Behaviors>
                            <controls:NumericTextAnimationBehavior Value="{Binding Number}"/>
                        </Label.Behaviors>
                    </Label>

CodeBehind:

public partial class ProfilePage : ContentPage
{
    public string Number { get; set; }
    public ProfilePage()
    {
        InitializeComponent();

        this.BindingContext = this;

        lblScore.Behaviors.Add(new NumericTextAnimationBehavior());
    }

    private void btnSetRandom_Clicked(object sender, EventArgs e)
    {
        Random randomizer = new Random();
        Number = randomizer.Next(1, 9999).ToString();

        lblScore.Text = Number;

    }
}

It's not worked on my project. I want to apply counter animation for label.

Thank you for your supports.


Solution

  • I searched INotifyPropertyChanged pattern and solved it.

    Model:

    public class ScoreViewModel : INotifyPropertyChanged
    {
        // boiler-plate
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        protected bool SetField<T>(ref T field, T value, string propertyName)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }
    
        // props
        private string number;
        public string Number
        {
            get { return number; }
            set { SetField(ref number, value, "Number"); }
        }
    }
    

    Page:

    public partial class ProfilePage : ContentPage
    {
        ScoreViewModel scoreViewModel = new ScoreViewModel();
        public ProfilePage()
        {
            InitializeComponent();
    
            lblScore.BindingContext = scoreViewModel;
        }
    
        private void btnSetRandom_Clicked(object sender, EventArgs e)
        {
            Random randomizer = new Random();
            scoreViewModel.Number = randomizer.Next(9999, 99999).ToString();
    
        }
    }
    

    Xaml:

     <Label x:Name="lblScore"  FontSize="24" Text="{Binding Number}">
        <Label.Behaviors>
            <controls:NumericTextAnimationBehavior Value="{Binding Number}"/>
        </Label.Behaviors>
    </Label>
    

    If you want to deep learn about INotifyPropertyChanged implementation, look here (Implementing INotifyPropertyChanged - does a better way exist?)