You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] EventToCommandBehavior does not pass EventArgs to command. #569
Using EventToCommandBehavior to bind Event to a Command, the EventArgs are not passed to the command.
[RelayCommand]
void Navigated(WebNavigatedEventArgs args) //this is called with args = null.
Stack Trace
Link to Reproduction Sample
Steps to Reproduce
Bind command to the WebView navigated event, without specifying the CommandParameter.
Write a command that has WebNavigatedEventArgs as parameter.
IMPORTANT:
When testing on another machine, that had older preview version (Using MAUI.Dependencies 6.0.4) the arguments were passed CORRECTLY to the command.
After updating the visual studio to the latest version, the problem started showing there as well.
Expected Behavior
The WebNavigatedEventArgs should not be null
Actual Behavior
The WebNavigatedEventArgs are null.
Basic Information
Using:
CommunityToolkit.MAUI version 1.2.0.
CommunityToolkit.Mvvm version 8.0.0.
MauiDependencies 6.0.486
Workaround
The event has the correct EventArgs.
You can always use ((MyModel)BindingContext).NavigatedCommand.Execute(args);
Or write your code there.
Or call a method that is not a command.
The command is generated correctly. The command gets called after the event is raised.
The parameters are null.
The most important thing during to test, is to not test with MAUI.Dependencies 6.0.4, but update them to 6.0.486.
Because in the older version this code was working correctly, and you will not see the problem.
@H-A-Hristov, the situation isn't the amount of code... It's that we follow different patterns, maybe I set up my project in a different way and can't reproduce the issue. So in order to avoid time waste we always ask a reproduction project, that we can just plug-and-play and reproduce. That said would be great if you attach or share a link to a repo that has the project with the issue (:
Came across this as well. After looking through the history of EventToCommandBehavior i found that eventArgs are no longer passed as a parameter without a converter. If you want to pass the eventargs create an converter inheriting from BaseConverterOneWay<EventArgsType, ReturnType> and add it as EventArgsConverter
Even object to object converter like this: public override object ConvertFrom(object value, CultureInfo culture) { return value; }
will get the job done.
(There are many ways to work around this, the question is - do we need fill our applications with such parasitic code, after all we are using this toolkit to write less, not more...)
Guys, we appreciate your efforts to create an issue, but we require reproduction source code (published on GitHub, not a zip archive) to continue investigating the issue.
More importantly, if you do not like this sample - it has only one line of code you have to add: <mct:EventToCommandBehavior EventName="Clicked" Command="{Binding IncrementCommand}" />
You will see, that you cannot get the event arguments passed to the command.
And most importantly, and I cannot stress this enough - no matter how many samples you try (uploaded by me, the community toolkit guys or someone else), the problem is not with this single line of code.
You will have to use the specific version of visual studio/maui dependencies, to reproduce it.
I have spent fair amount of my time, to test it on different machines, find what is wrong, and describe it to the best of my abilities.
[RelayCommand]privateasyncvoidWebViewNavigated(WebNavigatedEventArgse){if(e.Result!=WebNavigationResult.Success){// TODO: handle failed navigation in an appropriate wayawaitShell.Current.DisplayAlert("Navigation failed",e.Result.ToString(),"OK");}}
I was having the same issue, even using the EventArgsConverter Property, but my converter was not being called. The issue was that I was inheriting from IValueConverter instead of BaseConverterOneWay<>. Once I change that it started working.
Thanks
I was having the same issue, even using the EventArgsConverter Property, but my converter was not being called. The issue was that I was inheriting from IValueConverter instead of BaseConverterOneWay<>. Once I change that it started working.
Thanks
Oh I see that we do require our own Converter implementation which I think feels a bit wrong. I would expect this line
I think x:TypeArguments="WebNavigatedEventArgs" is all we need given that the type exists under Microsoft.Maui.Controls, I will aim to confirm this when I have VS open
Reacted by Edward Miller, Thomas Galliker and hyunq21
Activity
pictos commentedon Aug 29, 2022
@H-A-Hristov
Could you attach a small repro?
H-A-Hristov commentedon Aug 29, 2022
There really isn't too much of a code to test.
Adding this to WebView Behaviors in the XAML:
Adding this to the bound ViewModel:
The command is generated correctly. The command gets called after the event is raised.
The parameters are null.
The most important thing during to test, is to not test with MAUI.Dependencies 6.0.4, but update them to 6.0.486.
Because in the older version this code was working correctly, and you will not see the problem.
pictos commentedon Aug 30, 2022
@H-A-Hristov, the situation isn't the amount of code... It's that we follow different patterns, maybe I set up my project in a different way and can't reproduce the issue. So in order to avoid time waste we always ask a reproduction project, that we can just plug-and-play and reproduce. That said would be great if you attach or share a link to a repo that has the project with the issue (:
H-A-Hristov commentedon Aug 30, 2022
You can use the template for MAUI application that comes with visual studio 2022.
DSlutter commentedon Sep 5, 2022
Came across this as well. After looking through the history of EventToCommandBehavior i found that eventArgs are no longer passed as a parameter without a converter. If you want to pass the eventargs create an converter inheriting from BaseConverterOneWay<EventArgsType, ReturnType> and add it as EventArgsConverter
<mct:EventToCommandBehavior EventName="Event" Command="{Binding Command}" EventArgsConverter="{StaticResource EventArgsConverter}"/>Here is an example of a class from the Community Toolkit inheriting from BaseConverterOneWay
https://github.com/CommunityToolkit/Maui/blob/773ad5bab1e79c765892e0b14c8765769e8048b8/src/CommunityToolkit.Maui/Converters/ItemTappedEventArgsConverter.shared.cs
H-A-Hristov commentedon Sep 8, 2022
Even object to object converter like this:
public override object ConvertFrom(object value, CultureInfo culture) { return value; }will get the job done.
(There are many ways to work around this, the question is - do we need fill our applications with such parasitic code, after all we are using this toolkit to write less, not more...)
VladislavAntonyuk commentedon Sep 8, 2022
Guys, we appreciate your efforts to create an issue, but we require reproduction source code (published on GitHub, not a zip archive) to continue investigating the issue.
H-A-Hristov commentedon Sep 9, 2022
You have your sample.
This issue has nothing more, but the most basic sample that comes as documentation from the Community Toolkit.
https://github.com/CommunityToolkit/Maui/blob/main/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/EventToCommandBehaviorPage.xaml
More importantly, if you do not like this sample - it has only one line of code you have to add:
<mct:EventToCommandBehavior EventName="Clicked" Command="{Binding IncrementCommand}" />You will see, that you cannot get the event arguments passed to the command.
And most importantly, and I cannot stress this enough - no matter how many samples you try (uploaded by me, the community toolkit guys or someone else), the problem is not with this single line of code.
You will have to use the specific version of visual studio/maui dependencies, to reproduce it.
I have spent fair amount of my time, to test it on different machines, find what is wrong, and describe it to the best of my abilities.
mrlacey commentedon Sep 21, 2022
I found myself here as I thought I'd hit this too. However, based on the comment from @DSlutter I saw that the issue was a lack of EventArgsConverter.
The action here seems to be a need to better document this requirement.
@H-A-Hristov if it helps, here's some code from my project which gets this working.
The converter:
The View (partial)
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" xmlns:conv="clr-namespace:EmulatorApp28.Converters" > <WebView x:Name="webview" Source="{Binding Source}"> <WebView.Behaviors> <toolkit:EventToCommandBehavior EventName="Navigated" Command="{Binding WebViewNavigatedCommand}"> <toolkit:EventToCommandBehavior.EventArgsConverter> <conv:WebNavigatedEventArgsConverter/> </toolkit:EventToCommandBehavior.EventArgsConverter> </toolkit:EventToCommandBehavior> </WebView.Behaviors> </WebView>The ViewModel:
Add note for when EventArgsConverter is required
Add note for when EventArgsConverter is required
Sh3rlck commentedon Sep 26, 2022
I was having the same issue, even using the EventArgsConverter Property, but my converter was not being called. The issue was that I was inheriting from IValueConverter instead of BaseConverterOneWay<>. Once I change that it started working.
Thanks
bijington commentedon Sep 29, 2022
Oh I see that we do require our own Converter implementation which I think feels a bit wrong. I would expect this line
Maui/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehavior.shared.cs
Line 34 in bbf49ec
IValueConverterTheCodeTraveler commentedon Sep 29, 2022
Good catch, all! Agreed @bijington 💯
bijington commentedon Nov 7, 2022
To everyone involved in this (@mrlacey, @DSlutter, @Sh3rlck @H-A-Hristov) there is actually a way to achieve this without needing the converter. I will add some docs explaining it but for now you can see that we have a generic option for
EventToCommandBehaviorover at https://github.com/CommunityToolkit/Maui/blob/main/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehaviorGeneric.shared.csThis boils down to being able to define the example from @mrlacey as
I think
x:TypeArguments="WebNavigatedEventArgs"is all we need given that the type exists underMicrosoft.Maui.Controls, I will aim to confirm this when I have VS open