Bug 55716 - Bindable properties for reference types re-use default values
Summary: Bindable properties for reference types re-use default values
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Stephane Delcroix
URL:
Depends on:
Blocks:
 
Reported: 2017-04-28 10:29 UTC by Rhi
Modified: 2017-05-30 09:45 UTC (History)
4 users (show)

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


Attachments
Sample to reproduce the issue (68.56 KB, application/zip)
2017-04-29 05:48 UTC, Rhi
Details


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 ANSWERED

Description Rhi 2017-04-28 10:29:06 UTC
If a bindable property for a type such as a List<T> is created with a default of new List<T>, it seems to re-use the same container object for different unrelated instances of the BindableObject.
The effect here is that if there are 5 BindableObjects declared in XAML and all of them have 1 bindable property of reference type, they all end up have a bindable property with 5 objects, because they all use the same reference object provided in the default parameter.
Comment 1 Rhi 2017-04-29 05:48:18 UTC
Created attachment 21899 [details]
Sample to reproduce the issue

ExtendedToolbar is a custom control with a bindable property which is an IList of ToolbarItems.
If the property is declared with a default value, it seems to add 6 toolbar items to each container stacklayout,
whereas if the property is declared with a defaultValueCreator func param, things work as expected.

PS : Please do a restore packages since adding packages subfolder from the project takes it over the attachment size limit.
Comment 2 Paul DiPietro [MSFT] 2017-05-08 17:18:25 UTC
Thanks for the reproduction project. I will mark this as confirmed until there's further investigation.
Comment 3 Stephane Delcroix 2017-05-30 09:45:21 UTC
Yes, default values are reused.

If you use reference types, you should use the defaultValueCreator delegate instead, that will create a new default value. This is how we do it for, e.g. Grid.ColumnDfinition

https://github.com/xamarin/Xamarin.Forms/blob/d0cdd45bc3db1cb3b50bf4a866caca5f3bc958e2/Xamarin.Forms.Core/Grid.cs#L33