0

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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
amal50
  • 981
  • 2
  • 21
  • 35
  • 1
    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 Apr 24 '17 at 19:03
  • 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 Apr 24 '17 at 19:05

2 Answers2

2

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
    }
}
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
Ben Reierson
  • 1,099
  • 6
  • 8
  • Great, Thank you very much, but small fix, it must be (e.PropertyName == "TotalUniqueCount") – amal50 Apr 29 '17 at 01:04
0

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
 }
Nick Peppers
  • 3,161
  • 23
  • 27
  • since you posted this answer, I made a lot of research and reading trying to understand what you mean and where I can get the event when the binding of the label changes, I think this for setting the value of the label, not handle event when the value of the label changes, I already have label with binding changes when the variable changes, what I want is after changing the text value of the label to handle event and do another tasks, like retrieve data from the database. – amal50 Apr 26 '17 at 02:33
  • Maybe I'm not understanding what you're wanting to do, but when the text on TextChanged changes to a new value it would call OnTextChanged where you can still do other tasks like retrieve data. You'd just have to cast `var view = (YourView)bindable;` in OnTextChanged to get any of your views local references since it is static and from there you should be able to invoke any events/retrieve any data that you'd need. – Nick Peppers Apr 26 '17 at 16:57