Bug 36248 - Incorrect handling of BindableProperties of collection types in XAML
Summary: Incorrect handling of BindableProperties of collection types in XAML
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: unspecified
Hardware: Other Other
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-11-25 10:30 UTC by n.podbielski
Modified: 2016-02-12 12:44 UTC (History)
2 users (show)

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

sample application (29.05 KB, application/zip)
2015-11-25 10:30 UTC, n.podbielski

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:

Description n.podbielski 2015-11-25 10:30:32 UTC
Created attachment 13990 [details]
sample application

When control (TestControl in sample) have BindableProperty with collection type (i.e. ObservableCollection<Label> like TestCollection in sample) that have default value of null and it is added to Xaml page in following way:

        <Label Text="1"></Label>

Xaml parser throws critical exception:

Position 12:10. Cannot assign property "TestCollection": type mismatch between "Xamarin.Forms.Label" and "System.Collections.ObjectModel.ObservableCollection`1[Xamarin.Forms.Label]"

Instead of NullReferenceException, because Label cannot be added to collection when it is null. This way it works in XAML in WPF framework. 

Property XXX.ObservableCollection is null
            w System.Xaml.XamlObjectWriter.WriteGetObject()
            w System.Xaml.XamlWriter.WriteNode(XamlReader reader)
            w System.Windows.Markup.WpfXamlLoader.TransformNodes(...)

Moreover it is possible to do this:

        <objectModel:ObservableCollection x:TypeArguments="Label">
          <Label Text="1"></Label>

Which I doubt is usefull at all, since intuitive thing to do is to add items to collection not initiate collection itself first.  In WPF it is simply prohibited and it gave following error:

Error		A value of type 'ObservableCollection' cannot be added to a collection or dictionary of type 'ObservableCollection'.	XXX	XXXX\MainWindow.xaml	45

I think this is more sane behavior. Xamarin error is much more confusing.
Comment 1 Stephane Delcroix 2016-02-12 12:44:07 UTC
Your syntax will work if you initialize the collection to a non-null value.

Given the way our parser works, the reported message is as accurate as possible. Thanks for the suggestion for detecting this special case and handling it with a custom error message. We will consider it.

Warm regards,

the XF team