-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RelativeSource Binding Does Not Work For PlatformBehaviors (MCT Touch Behavior) #24313
Comments
Hi I'm an AI powered bot that finds similar issues based off the issue title. Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you! Open similar issues:
Closed similar issues:
|
@simonrozsival Hey I've opened a new issue seperate from #23711, there is a documented example of the crash. If theres anything I'm doing wrong wiring this up please let me know, I'm fairly sure it's correctly setup! |
I can repro this issue at Android/iOS platforms on the latest 17.12.0 Preview 1.0(8.0.80 & 8.0.72). |
This has been a fairly large pain point here as the base MAUI doesn't have a long press gesture and I desperately need one. |
@Axemasta I finally looked into the issue, sorry for the delay. The code throws because @StephaneDelcroix is this behavior expected? Both the |
OK, I think I can answer my own question: The best workaround that produces a binding that can be compiled I can think of at the moment is this: public partial class MyPage : ContentPage
{
public MyPage(ExampleViewModel exampleViewModel)
{
InitializeComponent();
BindingContext = exampleViewModel;
}
public ExampleViewModel ViewModel => (ExampleViewModel)BindingContext;
} <DataTemplate x:DataType="models:DisplayItem">
<Grid ColumnDefinitions="Auto,*,Auto" RowDefinitions="*,*">
<Grid.Behaviors>
<mct:TouchBehavior DefaultOpacity="1"
PressedOpacity="0.8"
PressedBackgroundColor="LightGray"
DefaultBackgroundColor="White"
Command="{Binding ViewModel.ItemSelectedCommand, Source={x:Reference Root}, x:DataType=viewmodels:ExampleViewModel}"
CommandParameter="{Binding .}"/>
</Grid.Behaviors>
<!-- ... -->
</Grid>
</DataTemplate> Alternatively, it is possible to force XamlC to skip compilation of that binding and ignore the warning it will produce: Command="{Binding BindingContext.ItemSelectedCommand, Source={x:Reference Root}, x:DataType=x:Null}" EDIT: I see there's a class MyPage : ContentPage
{
protected MyPage(ExampleViewModel vm)
{
base.BindingContext = vm;
}
public new ExampleViewModel BindingContext => (ExampleViewModel)base.BindingContext;
} |
@simonrozsival thanks for that explanation this is very useful. I have a PR open, but in a stagnant place regarding I've heard from the MCT team that |
Behaviour shouldn't have ever been BindableObject. I'm guilty of that. They're meant to be shared wo they can't have parents. The problem is that the documentation for Behaviour shows the usage of Behaviour as a BindableObject |
Any progress on this? The "golden" rule (if I understood this thread correctly or your suggestion from the other issue @simonrozsival ) would now be to always use Well, unless I'm mistaken that would mean to pollute our page xaml with additional 500+ lines of our DataTemplate xamls. or how am I gonna use the Reference unless it's within the same file, right? I'm not exactly thrilled by that thought 🫤 |
I ended up re-architecting my templates and did it in the backends
this only works if your viewmodels are globally scoped |
@DDHSchmidt No, you shouldn't need to make any changes to your code. You can safely use RelativeSource bindings. This issue is specific to the MCT touch behavior which currently cannot be used with RelativeSource bindings (in both .NET 8 and the upcoming .NET 9). If you have code that is using RelativeSource and it is working right now, it will continue working as is even when updating to .NET 9 for example. In the discussion in the other issue that you linked I was initially suggesting using RelativeSource instead of x:Reference, not the other way round if I remember correctly. Later on I described ways to continue using x:Reference for customers who were not able to switch to RelativeSource or in the cases like the MCT touch behaviors which currently cannot work with RelativeSource. Do you have any specific example of code that is causing you problems or that you think might be affected? I can certainly have a look and we can discuss it in more detail. I would suggest that you open a new issue though. Thanks! |
We do have code that is working right now since we recreated some of TouchBehavior's benefits via Effects in our own code. |
Currently the At some point I need to write up a discussion on this repo about Behaviors & Effects and what the long term plan is because currently TouchBehavior is unfixable 🫤 |
Description
Earlier this year the maui community toolkit shipped the
TouchBehavior
which was a port of the popularTouchEffect
, an issue noticed in development and by the community once released was thatRelativeSource
bindings cause the following exception to throw:Our workaround for this problem was to use
x:Reference
s to grab the binding context you needed, ie from the root page. This did leave users using shared data templates unsupported, however it supported most cases. Recently in #23711 it has been reported that compiled bindings will break the way our users are referencing their binding context. This is a major issue for the MCT, but possibly otherPlatformBehavior
s that use binding.I understand that I am reporting a bug from another library, however I cannot understand what the
TouchBehavior
is doing wrong and now seeing the workaround will be breaking in .NET 9, i thought it was time to report it and investigate where the issue lies. If the issue is on the MCT side please let us know why this happens so we can understand and prevent this issue in the future.I have a fully coded sample app demonstrating the issue.
Steps to Reproduce
See reproduction app for clerarer details:
TouchBehavior
to aDataTemplate
Command
, use aRelativeSource
Expected:
The relative binding applies and the command executes based on the touch behaviors interactions.
Actual:
The following crash occurs:
Last known working version is unknown for maui. I tested the
TouchEffect
in forms and relative source bindings did work then.Link to public reproduction project repository
https://github.com/Axemasta/TouchBehaviorRelativeSource
Version with bug
8.0.80 SR8
Is this a regression from previous behavior?
No, this is something new
Last version that worked well
Unknown/Other
Affected platforms
iOS, Android, Windows, macOS, Other (Tizen, Linux, etc. not supported by Microsoft directly)
Affected platform versions
All
Did you find any workaround?
Use
x:Reference
and hook onto a parent element, however this will break in .NET 9 along with compiled bindingsRelevant log output
No response
The text was updated successfully, but these errors were encountered: