Bug 24054 - BindableObject.ApplyBindings is broken when the old bindingcontext was null
Summary: BindableObject.ApplyBindings is broken when the old bindingcontext was null
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Eric Maupin
URL:
Depends on:
Blocks:
 
Reported: 2014-10-25 20:51 UTC by luca.barbieri
Modified: 2015-07-27 11:35 UTC (History)
6 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description luca.barbieri 2014-10-25 20:51:53 UTC
This is the decompiled code of BindableObject.ApplyBindings:

protected void ApplyBindings (object oldContext = null)
{
        BindableObject.BindablePropertyContext[] array = this.properties.ToArray ();
        for (int i = 0; i < array.Length; i++) {
                BindableObject.BindablePropertyContext bindablePropertyContext = array [i];
                BindingBase binding = bindablePropertyContext.Binding;
                if (binding != null) {
                        if (oldContext != null) {
                                binding.Unapply ();
                        }
                        binding.Apply (this.BindingContext, this, bindablePropertyContext.Property);
                }
        }
}


This is wrong, because calling binding.Unapply only when oldContext is null makes no sense.

oldContext being null has nothing to do with whether the binding was applied (you can/should be able to set bindings with null BindingContext just fine, by setting Source for instance), so binding.Apply fails with an exception complaining that the binding is being reused because binding.Unapply was not called.

You probably need to record whether bindings were applied using a private instance variable on BindableObject.

There probably other bugs in the code regarding null BindingContexts, stemming from the same misconception that it cannot be null.
Comment 1 Jason Smith [MSFT] 2015-07-16 17:44:55 UTC
Should be fixed in 1.4.4-pre1