Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Binding: Mode Two way works well without Break Point, but works strange with it.

EliranEliran USMember ✭✭✭

I have a an entry which is binded to a string in TwoWay mode.
When it runs without any breakpoints it works well.
When I put a break point in the set of the property and then erase some character in the entry,
it seems to erase all the characters one after an other, until ALL the characters are being erased (The break point is hit again and again and again
instead one time...)
Why is it?

This is the entry:
<Entry x:Name="myEntry" Text="{Binding TheFirstNameOfThePerson, Mode=TwoWay}" />

This is the C# code of the page that contains the entry:

    public partial class MainPage : ContentPage
        {
            public MainPageViewModel MainPageViewModel { get; set; }
            public MainPage()
            {
                InitializeComponent();
                MainPageViewModel = new MainPageViewModel();
                this.BindingContext = MainPageViewModel;
            }
        }

This is the viewModel:

public class MainPageViewModel:INotifyPropertyChanged
 {
    public Person Person { get; set; }
    public MainPageViewModel()
    {
        Person = new Person
       {
           Id = 100,
           FirstName = "Shabi"
       };
     }    

     public string TheFirstNameOfThePerson
     {
         get => Person.FirstName;
         set                   //Here I put the break point
         {
             Person.FirstName = value;
             OnPropertyChanged();
         }
     }

     public event PropertyChangedEventHandler PropertyChanged;
     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
     {
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
     }
 }

This is the model:

    public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
    }

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Xamarin.Forms controls subscribe to the PropertyChanged event of their BindingContext automatically, that is how binding works.

    When we checked the source code of PropertyChangedEventArgs, we will find:

    namespace System.ComponentModel
    {
        //
        // Summary:
        //     Represents the method that will handle the System.ComponentModel.INotifyPropertyChanged.PropertyChanged
        //     event raised when a property is changed on a component.
        //
        // Parameters:
        //   sender:
        //     The source of the event.
        //
        //   e:
        //     A System.ComponentModel.PropertyChangedEventArgs that contains the event data.
        public delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs e);
    }
    

    So, this function is triggered once the value of your bound property changes.

  • EliranEliran USMember ✭✭✭
    edited July 31

    @jezh Thank you for your answer.

    @jezh said:
    Xamarin.Forms controls subscribe to the PropertyChanged event of their BindingContext automatically, that is how binding works.

    This is correct. The question is why when I put a break point in the place I wrote "//Here I put the break point",
    why then this break point being hit endlessly? (endlessly = until the entry is empty).

    Without break point it works as it should (one character is deleted from the Entry and then one character is deleted
    from the object in the memory and everything is well)

    Why putting a break point makes a change to this behavior, and cause to this process to run again and again and again?

  • jezhjezh Member, Xamarin Team Xamurai

    The question is why when I put a break point in the place I wrote "//Here I put the break point",
    why then this break point being hit endlessly? (endlessly = until the entry is empty).

    The reason is that once we change the value of the binded property, the PropertyChanged will be triggered.

    Take a string for example, even if we change a char of it, the function will be called.
    This is the reason this process to run again and again and again when you remove one character one by one.

  • EliranEliran USMember ✭✭✭

    @jezh Yes, the problem is that I remove only one character. When I remove only one character
    I expect the function to be called only once.
    In what I describe, I remove only one character, but the function is being called again and again and again.
    It is called until the whole string in the Entry is vanished.

  • jezhjezh Member, Xamarin Team Xamurai
    edited August 4

    In what I describe, I remove only one character, but the function is being called again and again and again.

    As a test,I put the break point just as follows:

    When I tried to delete one character, the code marked by a breakpoint is executed only once.

    Not the case you said the function is being called again and again and again.

  • EliranEliran USMember ✭✭✭
    edited August 4
    @jezh Great! Now please click F5 (in order to continue the running). You will see that this break point will get hit again. Then click again F5, and this break point will get hit again..(every time you will see that an other character is erased, when you, as a user, wanted to erase only one character).
    It is important that the text in the entry will be longer than 1 character.
  • jezhjezh Member, Xamarin Team Xamurai

    That's the way it's designed.
    If we delete character one by one, the code marked by a breakpoint will been executed again and again.

    If you want to execute the code only one time,we can first select these characters and delete them in one time.

  • EliranEliran USMember ✭✭✭
    edited August 5

    @jezh > @jezh said:

    If we delete character one by one...

    But I don't delete character one by one. I delete only one character.

    Think about the next situation:
    You have in the entry the next text:
    abcdefghijklmnopqrstuvwxyz
    Now, I delete only the last character 'z'.
    I except the break point will get hit only once right? but in reality the break point will be hit 26 times.
    It will erase z, then it will erase y, then it will erase x...and in the end it will erase a.
    But I as a user deleted only the last character 'z' (I didn't delete more than one character - I didn't
    hold the BackSpace key keyboard for long press (In that situation I would have delete more than one
    character but I don't want that. I deleted only the last character by clicking one shor click on
    BackSpace key in the keyboard.)

    There is no reason to the break point to get hit more than 1 time, but if you try it you will find out,
    that after the first hit, if you continue the running (by clicking F5), the break point will get hit again,
    even although you deleted only one character 'z'

  • jezhjezh Member, Xamarin Team Xamurai
    edited August 6

    Now, I delete only the last character 'z'.
    I except the break point will get hit only once right? but in reality the break point will be hit 26 times.
    It will erase z, then it will erase y, then it will erase x...and in the end it will erase a.

    Sorry, I tried to remove the last character 'z' as you said, and the break point just got hit only once .
    We haven't come across this before. If it is convenient for you, could you please post a basic demo and the steps so that we can try to reproduce this question?

  • EliranEliran USMember ✭✭✭

    @jezh Is there any guide for how to prepare a demo in the forum?

  • jezhjezh Member, Xamarin Team Xamurai

    You can upload your demo in https://github.com/ and share your demo link here.

    Another way is to compress your demo and attach it right here.

  • EliranEliran USMember ✭✭✭

    @jezh HI, I attached a demo (First time I prepare a demo :) )

    What we see in the demo is this:
    1. The initial text is "Shabi".
    2. I erased the last character. The character "i" is being erased, and the breaking point is getting hit only once (as it should,
    and not as I described...BUT then I erased the next letter which is b)
    3. After erasing **only **"b" the break point is getting hit endlessly untill the App crushes (And again....I emphasize, I
    deleted ONLY b, and NOT 'b' 'a' 'h' 'S').

    Please wait between F5 and F5 about 10 seconds.
    I mean when a break point is getting hit, just wait 10 seconds before you continue the running with F5.

  • starman2049starman2049 Member
    edited August 8

    This is very unusual. I have bound thousands of properties and never seen this. That being said I always for every property use protection like this. Try it and see if it helps. In my opinion this type of protection is necessary, not nice to have:

    public string TheFirstNameOfThePerson
    {
       get => Person.FirstName;
       set                   //Here I put the break point
       {
          if (value != Person.FirstName
          {
             Person.FirstName = value;
             OnPropertyChanged();
          }
       }
    }
    
  • EliranEliran USMember ✭✭✭

    @starman2049 Thanks you for your answer.

    I added the protection you suggested (and I understand its purpose).
    Unfortunately it didn't solve the problem.

    Please pay attention, this problem occurs only when you use break points.
    In a normal running - everything is ok.

Sign In or Register to comment.