0
votes

I'm developing App which communicate with RFID Reader, I have the following Label which takes it's value from the Reader (when I click on the read button on the hardware)

<Label Text="{Binding Statistics.TotalUniqueCount}" />

I want to handle and event when the text value changed, it looks like the label doesn't has such event, is there any text view control that can handle the text change event? I tried to use Entry control, but the problem that when I read the RFID tags, it gives me the first and second time correct values, but the third time it gives me wrong value , and that's happens only when I use Entry. for example, I read 3 unique tags, it gives me first time 3, when I read more 2 tags, the number becomes 5, but when I read the third time another 3 tags, the number becomes 1. I put Entry and Label in the same page with the same Binding, the Label shows correct values and the Entry shows wrong values.

is there any solution to handle event when this binding (Statistics.TotalUniqueCount) changes?

2
Label doesn't have a text changed event because there is no way for the user to change a label's text. It can only be done in code. Since you are using binding, your Statistics class should implement INotifyPropertyChanged, and any classes that need to know about a new value can subscribe to the PropertyChanged event. - Jason
My problem that I'm using SDK, so I don't have much control on the models and classes, I have the properties like this and I have to deal with them - amal50

2 Answers

2
votes

As was mentioned in the comments, it looks like the Statistics class must implement INotifyPropertyChanged (that's how the binding is able to update the UI when the data changes). If that's the case, you should just subscribe to that same event in your code. Wherever you have access to that Statistics variable (in code bebhind or viewmodel), just do

Statistics.PropertyChanged += (o,e)=> 
{ 
    if (e.PropertyName == "TotalUniqueCount")
    {
      //do something
    }
}
0
votes

What you can do is on whatever page/view your label is on create a new bindableproperty that is set to your binding. Then add a propertyChanged method that will get called if the text on your label changes.

 public static BindableProperty TextChangedProperty = BindableProperty.Create(nameof(TextChanged), typeof(string), typeof(YourView), string.Empty,  propertyChanged:OnTextChanged);

 public string TextChanged
 {
     get { return (string)GetValue(TextChangedProperty); }
     set { SetValue(TextChangedProperty, value); }
 }

 static void OnTextChanged(BindableObject bindable, object oldValue, object newValue)
 {
    //Do stuff
 }