Bug 27280 - Behavior<T> generic constraint is not restrictive enough
Summary: Behavior<T> generic constraint is not restrictive enough
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-02-21 13:51 UTC by Keith Rome
Modified: 2015-03-05 06:05 UTC (History)
4 users (show)

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:

Description Keith Rome 2015-02-21 13:51:06 UTC
Behavior<T> has a generic constraint on T of where T:BindableObject. However the Behaviors property only exists on VisualElement, not all bindable objects. You should not be allowed to define a Behavior for an element type that can't make use of it.

I ran into this while attempting to create a behavior for listview cells (Behavior<Cell>), only to discover that Cell does not inherit from VisualElement and therefore cannot have behaviors attached.

My preference is to move the Behaviors property to Element from VisualElement, or at least mirror the functionality as a special case for Cell. Otherwise, the only way for us to use Behaviors in a cell is to use our own ViewCell custom template, where we can attach them to the ViewCell.View.

But short of expanding the feature to include Cell (and perhaps other elements), the generic constraint on the Behavior<T> class should be restricted to disallow non-viable behavior definitions.
Comment 1 Jason Smith [MSFT] 2015-03-05 06:05:57 UTC
It is viable to consider library authors will allow now VisualElement BindableObject Behaviors. This is why the restriction is more lax.