Maui: [BUG] TouchBehavior crash
Is there an existing issue for this?
- I have searched the existing issues
Did you read the “Reporting a bug” section on Contributing file?
- I have read the “Reporting a bug” section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
Current Behavior
I have a CollectionView like this
<CollectionView
IsVisible="{Binding isNotBusy}"
ItemsSource="{Binding CoursesAssigned}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:Course">
<Border
Margin="50"
Stroke="Purple"
StrokeThickness="1">
<Border.StrokeShape>
<RoundRectangle CornerRadius="8,8,8,8" />
</Border.StrokeShape>
<Border.Behaviors>
<mct:TouchBehavior Command="{Binding Path=GoToDetailsCommand, Source={x:RelativeSource AncestorType={x:Type vm:MyCoursesPageViewModel}}}"
CommandParameter="{Binding .}"
DefaultBackgroundColor="Transparent"
HoveredBackgroundColor="CornflowerBlue"
PressedBackgroundColor="CornflowerBlue" />
</Border.Behaviors>`
But when I run it, I get the error
Operation is not valid due to the current state of the object.
Expected Behavior
My command is declared in my viewModel
[RelayCommand]
async Task GoToDetails(Course course) {
await appService.NavigateTo($"{nameof(MyCoursesDetailPage)}",
true, new Dictionary<string, object> {
{"Course", course }
});
}
So I am expecting my command to go the MyCoursesDetailPage when I click the border
Steps To Reproduce
- Create a new Maui project
- Install the mct (Maui community toolkit)
- Create some sort of class to represent an object, like fruits.
- create a collectionView
- Create a border
- attach the Touch behavior to a border
Link to public reproduction project repository
https://github.com/eduardoagr/TouchBehaviorExample
Environment
- .NET MAUI CommunityToolkit:
- OS: Windows, Android
- .NET MAUI:
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 5
- Comments: 55 (23 by maintainers)
@eduardoagr & @r-work can you have a look at this post from @brminnick on SO. Here he shows exactly how to set the binding context on the behavior appropriately, I have seen the crashes when not setting it the recommended way too.
I’m currently in the process of updating the MCT samples & documentation to explicitly call this issue out and show the appropriate way to use the behavior, I have put this in my docs pr but the way you should setup the behavior to pass a command is here:
The checklist is:
x:Name="Pageon your root element (assumingContentPage)BindingContext:BindingContext="{Binding Source={x:Reference Page}, Path=BindingContext}"Commandnormally:Command="{Binding IncreaseTouchCountCommand}"I’ve opened an issue with maui because I’m convinced the
RelativeSourceshould be working… We will see that what say!It work for me:
8.0.14, thex:Referenceworks for me, I’m talking about the relative bindingCommand="{Binding Source={RelativeSource AncestorType={x:Type vm:MyViewModel}}, Path=MyCommand}"not working.This is definitely a regression from XCT since my Maui TouchEffect was able to use relative bindings, I noticed this issue when removing my port from my apps.
Binding issues will be fixed in the next release, see #1791 for details.
The
TouchBehaviorfrom this release will inherit theBindingContextof theVisualElementit is attached to (if it exists). This means it will behave in the same way the XCT effect did & the way my Maui.TouchEffect port did. I believe the change will be hotfixed so it should be available soon!Why do we have to use a name for the page, I always reference the vm, and that is what I was trying to do
Are we sure
RelativeSource’s in maui are working the way they are intended to? I feel like this behaviour should be a bug, if it isn’t a bug we need to support it properly because data templates do become very annoying to work with when using the MCT version!So you are using this inside a DataTemplate? Yes I don’t think that will work right now
this is how you can fix this.
As a workaround, you could do something like
` <CollectionView Grid.Row="1" Grid.ColumnSpan="3" Margin="5" IsVisible="{Binding isNotBusy}" ItemsSource="{Binding CoursesAssigned}">
</Grid>`
Can everyone please update to 8.0.1, this release should fix all the issues we are seeing with data binding!
You can migrate normally now, I’ve added a little XCT to MCT migration guide on the TouchBehavior docs incase it is of use to anyone!
To answer both of your questions @eduardoagr, @r-work we have to reference something else on the view, ie the page or indeed a parent view because the
TouchBehavioritself has noBindingContext. In XCT it did because it was an attached effect, the current MCT implementation uses aPlatformBehaviorwhich does not set theBindingContext, this is following Maui documentation guidelines (see article).So whilst other things like
TapGestureRecognizerwill work as expected, theTouchBehaviorwon’t unless you set its binding context through anx:Referenceand invoke it that way.Same issue, except I’m adding the touch behavior to a
Frame.This seems to be related to the
Commandproperty binding, if you do not set it, it won’t crash.Output window: