Touch Behavior Should Automatically Set BindingContext #1785
Replies: 3 comments 6 replies
-
For inspiration we can use the code on our I like the idea of having a bool flag to control it, that way we will not break anyone. I would that flag should NOT be bindable, this will be less flexible for users but will make our life easier. Also I can't see a place where they will change that value too often during runtime, if the user needs that scenario he can implement that prop. as bindable |
Beta Was this translation helpful? Give feedback.
-
Edit I'm wrong. We already automatically set the I strongly recommend against auto-setting the The .NET MAUI team purposefully chose for Behaviors to not inherit their attached view's Yes, I agree, it's annoying for folks to open "Bugs" on our repo about it. Yes, I agree, it's different functionality than Xamarin.Forms. Yes, I agree, this would likely make life easier for many developers. I understand that auto-setting Behavior.BindingContext would be well-intended, but we should not purposefully break .NET MAUI's paradigm. Behaviors are new to many developers who previously used Effects in Xamarin.Forms. It will take time for .NET MAUI developers to learn about the nuance of To demonstrate how auto-setting a Behavior's BindingContext could cause unintended consequences, here's an example: class MainPage : ContentPage
{
public MainPage()
{
var touchBehavior = new TouchBehavior();
var label = new Label();
label.Behaviors.Add(touchBehavior); // <-- If we auto-set `TouchBehavior.BindingContext`, TouchBehavior.BindingContext == Label.BindingContext
var entry = new Entry();
entry.Behaviors.Add(touchBehavior); // <-- If we auto-set `TouchBehavior.BindingContext`, now TouchBehavior.BindingContext == Entry.BindingContext. Is the developer expecting this? Is this what they intended? Likely not.
Content = new VerticalStackLayout
{
Children =
{
label,
entry
}
};
}
} |
Beta Was this translation helpful? Give feedback.
-
Closed as answered, if closed in error please let us know to re-open. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Now we have delivered the first cut of the
TouchBehavior
, I believe we need to seriously discuss adding a way to automatically set theBindingContext
of the behavior, based on theBindingContext
of the bindables the behavior is attached to.When we developed the idea we chose not to set the
BindingContext
based on the maui documentation which is quoted:When using the
TouchBehavior
we are asking out consumers to manually set theBindingContext
of the behavior. The reason maui call this out is because behaviors are likely to be shared in styles so obviously this introduces risks to having a sharedBindingContext
. The context is different for theTouchBehavior
where a new instance is applied for every view using it. We could make the following additions to the feature:AutoSetBindingContext
which is abool
. Iftrue
on attaching the platform implementation will set theBindingContext
of theTouchBehavior
instance to theVisualElement
passed into the effect as per thePlatformBehavior<VisualElement>
contract.TouchBehavior
in shared styles. I believe the auto bindable approach would avert the issue of shared binding contexts since they will auto inherit the context of the viewI can see issues like #1783 being a common complaint with this feature in its current form and it will make using the
TouchBehavior
more like using the rest of Maui, I've personally never had to worry aboutBindingContexts
to this degree before!Code changes to the behavior are trivial, adding a new bindable property and the following lines to each platform implementation:
I have tested this locally by removing the manual binding context setting & setting
AutoSet
to true and the sample works as expected!Beta Was this translation helpful? Give feedback.
All reactions