-
Notifications
You must be signed in to change notification settings - Fork 1.9k
BindingContext changes are called multiple times on app start #470
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually hides a part of the issue*, but I'm ok to go with it, as it fixes the issue at hand, provided you add a unittest for it.
[*] full story here:
- Bindings are applied on SetInheritedBindingContext even when the context does not change => you fixed this
- Bindings are applied twice on Parenting; once in set_Parent and a second time in OnChildAdded. => I'm fine if we do not fix this
- This is not a bug, merely an improvement. We should be able to poke the binding source as often as we find convenient :)
child.Parent = this; | ||
if (Platform != null) | ||
child.Platform = Platform; | ||
|
||
child.ApplyBindings(); | ||
if(bindingContext == child.BindingContext) | ||
child.ApplyBindings(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, setting Parent
should set child binding context, but there are two checks in parent set that return before SetChildInheritedBindingContext()
is called. It seemed safer to add this check instead of removing apply bindings altogether. I'll work on unit testing tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's actually safer to leave this part of the code unchanged, and stay with the safe first version of the PR
This reverts commit 71952f3.
This still happens in v 2.3.4.231 |
This is merged with what? With master, that's fine, but with which Xamarin.Forms release, because for me it's also not working at 2.3.4.x. |
@Szymaniuk this was released in the 2.3.4 series. With this PR, and e.g. #983, we're reducing the number of times the Binding gets its source. We still do not consider it a bug. See #470 (review) for explanation. If you've a case when the Binding is updated when you think it shouldn't, please report a bug to bugzilla, we'll see if this is fixable or not. |
@StephaneDelcroix, thanks for the update. In my case, after updating to 2.3.4, the number of times my ViewModelLocator was called dropped from 3 to 2. However, I think I still need a little more explanation. Sorry if I didn't fully understand your review above. As described in the bugzilla reports (1) (2), if I set the BindingContext in XAML, my ViewModelLocator will be called multiple times.
If I set the BindingContext in the page's constructor in the codebehind, my ViewModelLocator is only called once.
Are you saying that is normal, expected behavior? If I want or need it to only be called once, the solution is to just set it in the page's constructor instead? |
@j-fritsch this isn't a wrong behavior, but #983 is supposed to address this anyway. |
Description of Change
If you put a view model locator as an App resource and set your binding context in MainPage, the locator gets called multiple times. This happens first when XAML for the main page is being loaded and then when the page is being parented by App.
This PR ensures that binding context changes are not made if the inherited context is the same as the value passed to
SetInheritedBindingContext()
Bugs Fixed
PR Checklist