Skip to content

[Bug] EventToCommandBehavior does not pass EventArgs to command. #569

Closed

Description

@H-A-Hristov

Description

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

  1. Bind command to the WebView navigated event, without specifying the CommandParameter.
  2. 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.

Reproduction imagery

Activity

  1. added
    bug
    on Aug 29, 2022
  2. pictos commented on Aug 29, 2022

    @pictos
    Member

    @H-A-Hristov
    Could you attach a small repro?

  3. H-A-Hristov commented on Aug 29, 2022

    @H-A-Hristov
    Author

    There really isn't too much of a code to test.

    Adding this to WebView Behaviors in the XAML:

    <toolkit:EventToCommandBehavior
                    EventName="Navigated"
                    Command="{Binding NavigatedCommand}" />
    

    Adding this to the bound ViewModel:

    [RelayCommand]
    void Navigated(WebNavigatedEventArgs args)
    

    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.

  4. pictos commented on Aug 30, 2022

    @pictos
    Member

    @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 (:

  5. H-A-Hristov commented on Aug 30, 2022

    @H-A-Hristov
    Author

    You can use the template for MAUI application that comes with visual studio 2022.

  6. DSlutter commented on Sep 5, 2022

    @DSlutter

    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

  7. H-A-Hristov commented on Sep 8, 2022

    @H-A-Hristov
    Author

    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...)

  8. VladislavAntonyuk commented on Sep 8, 2022

    @VladislavAntonyuk
    Contributor

    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.

  9. H-A-Hristov commented on Sep 9, 2022

    @H-A-Hristov
    Author

    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.

  10. mrlacey commented on Sep 21, 2022

    @mrlacey

    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:

    using CommunityToolkit.Maui.Converters;
    
    public class WebNavigatedEventArgsConverter : BaseConverterOneWay<WebNavigatedEventArgs, object>
    {
        public override object DefaultConvertReturnValue { get; set; }
    
        public override object ConvertFrom(WebNavigatedEventArgs value, CultureInfo culture = null) => value switch
        {
            null => null,
            _ => value
        };
    }

    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:

        [RelayCommand]
        private async void WebViewNavigated(WebNavigatedEventArgs e)
        {
            if (e.Result != WebNavigationResult.Success)
            {
                // TODO: handle failed navigation in an appropriate way
                await Shell.Current.DisplayAlert("Navigation failed", e.Result.ToString(), "OK");
            }
        }
  11. Sh3rlck commented on Sep 26, 2022

    @Sh3rlck

    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

  12. bijington commented on Sep 29, 2022

    @bijington
    Contributor

    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

    BindableProperty.Create(nameof(EventArgsConverter), typeof(ICommunityToolkitValueConverter), typeof(EventToCommandBehavior));
    to expect a type of IValueConverter

  13. TheCodeTraveler commented on Sep 29, 2022

    @TheCodeTraveler
    Collaborator

    Good catch, all! Agreed @bijington 💯

  14. bijington commented on Nov 7, 2022

    @bijington
    Contributor

    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 EventToCommandBehavior over at https://github.com/CommunityToolkit/Maui/blob/main/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehaviorGeneric.shared.cs

    This boils down to being able to define the example from @mrlacey as

    <WebView x:Name="webview" Source="{Binding Source}">
        <WebView.Behaviors>
            <toolkit:EventToCommandBehavior
                x:TypeArguments="WebNavigatedEventArgs"
                EventName="Navigated"
                Command="{Binding WebViewNavigatedCommand}" />
        </WebView.Behaviors>
    </WebView>

    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

  15. locked and limited conversation to collaborators on Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds reproductionunverified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions