Closed TheBlueSky closed 4 years ago
This is normal.
1/ the DataTemplate is inflated, so the view (stack layout) is created, and the bindings are evaluated 2/ the StackLayout is parented, and as such, the bindingContext (potentially) changes, so bindings are applied 3/ the context is set (to the letter a to h), so bindings are once again evaluated.
your code shouldn't rely on the number of times a converter is invoked...
We could enhance this a little bit, by swapping steps 2 and 3 (bindings won't be reapplied when parented) (/cc @hartez is this possible), but we would keep the double evaluation, like all bindings created by XAML nowadays.
There's an enhancement for dotnetMaui to change the way contexts are resolved that could make this a one-stepper, but nothing we can change right now.
This is normal.
@StephaneDelcroix, I do not think this is entirely accurate though.
Let us make a tiny modification to the code I have in my bug reports. Let us change {Binding ., Converter={StaticResource StringToLowerCaseConverter}}
to {Binding Length, Converter={StaticResource StringToLowerCaseConverter}}
; in other words, instead of binding to the item itself, we bind to a property of an item, Length
in this case.
Now when I debug the code I have the StringCaseConverter.Convet()
called exactly 8 times, as expected.
I do not think this is entirely accurate though.
It is still accurate. when specifying a Path, the Binding is still evaluated as often as mentioned earlier, but as the Path can't be resolved (BindingContext is null) the converter isn't evaluated.
My advice again: make sure your converters are cheap to execute, and with no side effects.
My converter is cheap and has no side effect. I was mainly wondering about the behaviour that does not seem intuitive at first, especially coming from a previous experience with WPF, Windows Phone, and UWP.
I still think this can be enhanced so that the code behaviour is as it looks, but thank a lot for clarifying it.
I've also seen my converters being invoked a lot more than I would have assumed. If there's any optimisations that can be done in regard to the number of times bindings are evaluated I think that'd be a big performance win, especially if WPF works differently. Thanks.
Description
Converter is called 2 times more than the number of items on the list.
Steps to Reproduce
StringCaseConverter
belowvar stringValue = value?.ToString() ?? string.Empty;
StringCaseConverter.cs
App.xaml
MainPage.xaml
Expected Behavior
The breakpoint is hit 8 times.
Actual Behavior
The breakpoint is hit 24 times.
Basic Information
Screenshots
N/A
Reproduction Link
See above.
Workaround
None that I know of.