Events and Commands in .NET MAUI AI AssistView (SfAIAssistView)

ItemTapped Event and Command

The SfAIAssistView control provides the ItemTapped and ItemTappedCommand to respond when an item is tapped. The tapped item and its position are passed through the ItemTappedEventArgs. This argument provides the following details:

  • Item : The tapped item within AI AssistView.
  • Position : The touch position when the item was tapped.

ItemTapped Event

The following example demonstrates how to handle the ItemTapped event.

<syncfusion:SfAIAssistView x:Name="aiAssistView"
                               ItemTapped="OnAssistViewItemTapped" />
private void OnAssistViewItemTapped(object sender, ItemTappedEventArgs e)
    {  
       DisplayAlert("Item", " Tapped on item :" + e.Item.Text, "Ok");                  
    }

ItemTapped Command

To handle the tap action using commands (MVVM), bind the ItemTappedCommand.

<syncfusion:SfAIAssistView x:Name="aiAssistView"  
                               ItemTappedCommand="{Binding TappedCommand}" />
public class ViewModel : INotifyPropertyChanged
    {
        public Command<object> tappedCommand;

        public ViewModel()
        {
            TappedCommand = new Command<object>(ExecuteItemTappedCommand);
        }
        
        public Command<object> TappedCommand
        {
            get { return tappedCommand; }
            set { tappedCommand = value; }
        }

        private void ExecuteItemTappedCommand(object obj)
        {
           var ItemTappedArgs = obj as ItemTappedEventArgs;
           DisplayAlert("Item", " Tapped on item :" + ItemTappedArgs.Text, "Ok");                  
        }    
      
    }

ItemLongPressed Event and Command

The SfAIAssistView control also supports the ItemLongPressed event and ItemLongPressedCommand, which are triggered when item is long-pressed. The ItemLongPressedEventArgs argument provides the following details:

  • Item : The item that was long-pressed.
  • Position : The touch position of the long press.

ItemLongPressed Event

<syncfusion:SfAIAssistView x:Name="aiAssistView"
                               ItemLongPressed="OnAssistViewItemLongPressed" />
private void OnAssistViewItemLongPressed(object sender, ItemLongPressedEventArgs e)
    {  
       DisplayAlert("Item", " Long pressed on item :" + e.Item.Text, "Ok");                  
    }

ItemLongPressed Command

To handle the long-press action using commands (MVVM), bind the ItemLongPressedCommand.

<syncfusion:SfAIAssistView x:Name="aiAssistView"
                               ItemLongPressedCommand ="{Binding LongPressedCommand}" />
public class ViewModel : INotifyPropertyChanged
    {
        public Command<object> longPressedCommand;

        public ViewModel()
        {
            LongPressedCommand = new Command<object>(ExecuteItemLongPressedCommand);
        }

        public Command<object> LongPressedCommand
        {
            get { return longPressedCommand; }
            set { longPressedCommand = value; }
        }

        private void ExecuteItemLongPressedCommand(object obj)
        {
            var ItemLongPressedArgs = obj as ItemLongPressedEventArgs;
            DisplayAlert("Item", "LongPressed on Item :" + ItemLongPressedArgs.Item.Text, "Ok");
        }
    }

Request Event and Command

The Request event and RequestCommand are triggered when a request item is sent. The RequestEventArgs arguments provides the following details:

  • RequestItem : Represents the item requested.
  • Handled : Indicates whether the request has been handled.

Request Event

The Request event is triggered when a request item is sent. This event can be handled to fetch response from an AI service.

<syncfusion:SfAIAssistView x:Name="aiAssistView"
                               Request="OnAssistViewRequest" />
private void OnAssistViewRequest(object sender, RequestEventArgs e)
    {  
       // Add your code for getting response from the AI.
    }

Request Command

To handle the request action using commands (MVVM), bind the RequestCommand.

<syncfusion:SfAIAssistView x:Name="aiAssistView"
                              AssistItems="{Binding AssistItems}" 
                              RequestCommand="{Binding AssistViewRequestCommand}"/>
public class ViewModel : INotifyPropertyChanged
 {
     ...

    public ViewModel()
    {
        this.assistItems = new ObservableCollection<object>();
        this.AssistViewRequestCommand = new Command<object>(ExecuteRequestCommand);
    }

    private async void ExecuteRequestCommand(object obj)
    {
        var request = (obj as Syncfusion.Maui.AIAssistView.RequestEventArgs).RequestItem;
        //logic for getting response from the AI
    }

    ....

}

ItemCopy Command

The ItemCopyCommand is executed when user clicks on the copy action icon in a response item.

<syncfusion:SfAIAssistView x:Name="aiAssistView"  
                               ItemCopyCommand="{Binding CopyCommand}" />
public class ViewModel : INotifyPropertyChanged
    {
        public Command<object> copyCommand;

        public ViewModel()
        {
            CopyCommand = new Command<object>(ExecuteCopyCommand);
        }
        
        public Command<object> CopyCommand
        {
            get { return copyCommand; }
            set { copyCommand = value; }
        }

        private void ExecuteCopyCommand(object obj)
        {
             // add copy logic here
        }    
      
    }

ItemRetry Command

The ItemRetryCommand is executed when user clicks on the retry action icon in a response item.

<syncfusion:SfAIAssistView x:Name="sfAIAssistView"  
                               ItemRetryCommand="{Binding RetryCommand}" />
public class ViewModel : INotifyPropertyChanged
    {
        public Command<object> retryCommand;

        public ViewModel()
        {
            RetryCommand = new Command<object>(ExecuteRetryCommand);
        }
        
        public Command<object> RetryCommand
        {
            get { return retryCommand; }
            set { retryCommand = value; }
        }

        private void ExecuteRetryCommand(object obj)
        {
            // add retry logic here
        }    
      
    }

ItemRatingChanged Command

The ItemRatingChangedCommand is executed whenever a user changes the rating of a response item. This typically occurs when a user performs an action like liking or disliking the item.

<syncfusion:SfAIAssistView x:Name="aiAssistView"   
                               ItemRatingChangedCommand="{Binding RatingChangedCommand}" />
public class ViewModel : INotifyPropertyChanged
    {
        public Command<object> ratingChangedCommand;

        public ViewModel()
        {
            RatingChangedCommand = new Command<object>(ExecuteRatingChangedCommand);
        }
        
        public Command<object> RatingChangedCommand
        {
            get { return ratingChangedCommand; }
            set { ratingChangedCommand = value; }
        }

        private void ExecuteRatingChangedCommand(object obj)
        {
            DisplayAlert("Rating", "Rating changed", "Ok");                              
        }    
      
    }