What do I need to do to migrate Xamarin-based EventToCommandBehavior command binding to work for .NET MAUI?

dg2k 1,396 Reputation points
2022-07-28T20:58:10.167+00:00

I have Xamarin-based reusable controls based on ContentView. One use case is for password input and encryption key input built around a flexible custom control that can be configured for user screen lock password enable/change/disable as well as data encryption key enable/change/disable. This works fine with Xamarin without any issues.

When I attempted to port the same code to .NET MAUI there is one notable issue although a workaround exists. The EventToCommandBehavior command binding to Mouse Click event returns null on MAUI-WinUI while expected value is returned for Xamarin-UWP. If however the Click Event is routed to code-behind (rather than binding to ViewModel with EventToCommandBehavior ) it works as expected. This is the workaround referred to.

Surely, something needs to be tweaked for EventToCommandBehavior to work on MAUI-WinUI as I see a clear difference between Xamarin and .NET MAUI.

<controls:LoginControl  
    ButtonCancelLabel="Cancel"  
    ButtonOkLabel="OK"  
    IsErrorMessageVisible="{Binding IsPwdErrorMessageVisible}"  
    OnPasswordEnter="PwdControl_OnPasswordEnter"  
    PwdValidationMode="{Binding LoginControlValidationMode, Mode=TwoWay}">  
    <controls:LoginControl.Behaviors>  
        <b:EventToCommandBehavior Command="{Binding OnLoginControlOkButtonClickedCommand}" EventName="OnPasswordEnter" />  
    </controls:LoginControl.Behaviors>  
</controls:LoginControl>  
  • LoginControl is a reusable custom control based on ContentView.
  • OnPasswordEnter is public event EventHandler OnPasswordEnter; in the custom control which is triggered by OnPasswordEnter?.Invoke(this, <event-info>).
  • The preferred way to capture the OnPasswordEnter mouse click event is with command binding to ViewModel (line 7 to 9) but this returns null for MAUI-WinUI. Capturing the event in code-behind (line 5) works but not ideal.

Can you suggest what changes I need to make for EventToCommandBehavior to work on MAUI-WinUI just like for Xamarin?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,412 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 30,666 Reputation points Microsoft Vendor
    2022-08-03T09:06:56.32+00:00

    Hello anonymous user-4673 ,

    Please try to copy this EventToCommandBehavior class and BehaviorBase class to your project.

    namespace QuestionAndAnswer.Demo.Behaviors  
    {  
             public class EventToCommandBehavior : BehaviorBase<VisualElement>  
             {...}  
    }  
    

    Then you can use it on your DemoPage, it works on my side, the EventArgs object won't be null in OnButtonTapped method of DemoPageViewModel.

      <ContentPage  
         ...  
         xmlns:behavior ="clr-namespace:QuestionAndAnswer.Demo.Behaviors"  
        ...>  
         <StackLayout VerticalOptions="CenterAndExpand">  
            ...  
             <demo:DemoUserControl  
                 ...>  
                 <demo:DemoUserControl.Behaviors>  
                     <behavior:EventToCommandBehavior Command="{Binding OnButtonTappedCommand}" EventName="OnPasswordButtonTapped" />  
                 </demo:DemoUserControl.Behaviors>  
             </demo:DemoUserControl>  
    ...  
    </ContentPage>  
    

    I checked your sample, you are using CommunityToolkit, it is not supported on Q&A, you can report this at https://github.com/CommunityToolkit/Maui.

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.