Can I use RadEventToCommandBehavior on the Page Level?

1 Answer 16 Views
General Discussions
Comercializadora Paxia
Top achievements
Rank 1
Iron
Comercializadora Paxia asked on 22 Mar 2024, 11:54 PM

Hello, 

I trying to use RadEventToCommandBehavior at page level to binding a Appearing Event to a command in my ViewModel, but it doesn't work. 

[Page.XAML]

 <ContentPage.Behaviors>
     <telerik:RadEventToCommandBehavior Command="{Binding AppearingCommand}" EventName="Appearing"  />
 </ContentPage.Behaviors>

[ViewModel]

[RelayCommand]
public async Task AppearingCommand()
{
    await LoadData();
}

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 25 Mar 2024, 05:37 PM

Hi Comercializadora Paxia,

I am not able to reproduce your issue. Please note that I am using a normal Command, and do not have any custom code generation to ensure clean debugging procedures.

I recommend that you double check that your automatic code generation is working as you expect it to be and that your BindingContext is correct. Go to the Microsoft Documentation for more information => How It Works.

For example, async Task is not the same as void and you are probably getting a signature mismatch IAsyncRelayCommand. is not the same as RelayCommand.

Working Example

public class MainViewModel
{
    public MainViewModel()
    {
        AppearingCommand = new Command(() =>
        {
            Debug.WriteLine("AppearingCommand Executed");
        });    
    }

    public Command AppearingCommand { get; set; }
}

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
             x:Class="MauiDemo.MainPage">
    <ContentPage.Behaviors>
        <telerik:RadEventToCommandBehavior Command="{Binding AppearingCommand}" EventName="Appearing"/>
    </ContentPage.Behaviors>

    <Grid>
    </Grid>
</ContentPage>

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        this.BindingContext = new MainViewModel();
    }
}

and at runtime, the correct execution is observed:

Conclusion

As I have confirmed the RadEventToCommandBehavior is working as expected with Command, you can pursue the problems with using non-standard commands, such as IAsyncRelayCommand and the code it generates.

you can use the following to generate the 

[RelayCommand]
private void GreetUser()
{
    Console.WriteLine("Hello!");
}

Note: If you require AsyncCommand, then you will need to use a different behavior, for example the same toolkit you are already using has one, too EventToCommandBehavior - .NET MAUI Community Toolkit   | Microsoft Learn

Regards,
Lance | Manager Technical Support
Progress Telerik

A brand new ThemeBuilder course was just added to the Virtual Classroom. The training course was designed to help you get started with ThemeBuilder for styling Telerik and Kendo UI components for your applications. You can check it out at https://learn.telerik.com
Tags
General Discussions
Asked by
Comercializadora Paxia
Top achievements
Rank 1
Iron
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or