question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bindings are executed twice

See original GitHub issue

Description

It appears that XAML bindings are being executed twice in certain circumstances. I’ve got several different situations in my MAUI application where this appears to be happening. At the moment I can only reproduce the following in a greatly simplified situation.

If I place a binding at the top of a Page definition, all the bindings on that page will we executed 2 times. It also appears that any IValueConverter attached to the binding is executed AFTER the lower bindings are applied the 1st time.

Thus if we have the following code:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:converters="clr-namespace:MauiBugz.Converters"
             xmlns:views="clr-namespace:MauiBugz.Views"
             BindingContext="{Binding Property, Mode=OneTime, Converter={x:Static converters:CountingConverter.Instance}, ConverterParameter='TopOfPage'}"
             x:Class="MauiBugz.Views.IssueView"
             Title="IssueView">

    <VerticalStackLayout>

        <ContentView
            BindingContext="{Binding Property, Mode=OneTime, Converter={x:Static converters:CountingConverter.Instance}, ConverterParameter='OneLevelDown'}"/>

        <ContentView
            BindingContext="{Binding Property, Mode=OneTime, Converter={x:Static converters:CountingConverter.Instance}, ConverterParameter='CustomContent'}"/>

    </VerticalStackLayout>
    
</ContentPage>

We will see the following progression:

[DOTNET] IssueViewModel Get Property
[DOTNET] OneLevelDown converter has been executed 1 times. Value: Data_A
[DOTNET] IssueViewModel Get Property
[DOTNET] CustomContent converter has been executed 1 times. Value: Data_A
[DOTNET] IssueViewModel Get Property
[DOTNET] TopOfPage converter has been executed 1 times. Value: Data_A          <<<<<<<<<<<<<< 
[DOTNET] ConvertedData_A Get Property
[DOTNET] OneLevelDown converter has been executed 2 times. Value: Data_B
[DOTNET] ConvertedData_A Get Property
[DOTNET] CustomContent converter has been executed 2 times. Value: Data_B

Note that TopOfPage converter is executed after all the lower bindings have been presented with the VM applied to the page. They were never to see the VM, they were to receive ConvertedData objects that were derived from the Data object returned from the VM.Property getter. (As you see in the 2nd half of the console output.)

Steps to Reproduce

  1. Clone the Repro
  2. Select the button for the Issue
  3. View the Console output.

NOTE: This issue is a bit tedious to illustrate via output as Bindings are relatively verbose processes with several steps. I’ve implemented the following in an attempt to help track the issue via console output.

CountingConverter is an IValueConverter implementation with the following:

  • Static Instance property that returns a unique/new converter for each access. (Each binding has it’s own instance)
  • ConverterParameter is expected to be a string “name” to uniquely identify each converter.
  • Designed to count each time it’s executed and prints it’s name, the count, and the value and it’s provided to the Console.
  • Takes a Data object in, and returns a ConvertedData object.

The initial ViewModel, Data, and ConvertedData all share the same Property property to simplify coding down the XAML tree.

  • The getter creates a new Data object on first access.

Data and ConvertedData get a new designation character when they are created to make each unique.

Link to public reproduction project repository

https://github.com/bakerhillpins/Issues/tree/NetMauiIssue10806

Version with bug

6.0.486 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

All

Did you find any workaround?

It appears that if you insert ContentView between your actual XAML and the Page (effectively wrapping your view) and move the TopOfPage binding down to that wrapper ContentView, the bindings are only executed once.

The Caveat here is that I’m actually using this workaround in my larger application, however, I’m still seeing the issue and have not been able to determine why at the moment.

Relevant log output

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
msftbot[bot]commented, Oct 20, 2022

We’ve moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

0reactions
bakerhillpinscommented, Jan 4, 2023

I’ve added a fix for this to the PR above, https://github.com/dotnet/maui/pull/12060

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is the Binding Format event called twice?
I am playing around with data binding and noticed that the Binding Format is called twice upon loading the form in the code...
Read more >
Operation Binding is executed twice
Dear All, I created an Action Binding in my pageDef file. This will call the web service interface. ... Operation Binding is executed...
Read more >
custom binding will trigger twice
if i bind two observable to a custom binding i set one observable ,the update function will be called twice it's don't make...
Read more >
Binding Set Property Called Twice ! in UI for Silverlight
Hi All, I'm using rad grid view having one column. I've binded using DataTemplate like this, < telerikGridView:RadGridView ...
Read more >
A Binding Formatter is Called Twice - thecave
It seems that a binding formatter is called twice when a user leaves a textbox control. As the user leaves the control the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found