Events and Commands in .NET MAUI Chat (SfChat)
MessageTapped Event and Command
The SfChat
control includes a built-in event called MessageTapped and a command named MessageTappedCommand. These are triggered when a message is tapped. You can access the tapped message and the point of interaction through the MessageTappedEventArgs.
MessageTapped Event
<sfChat:SfChat x:Name="sfChat"
MessageTapped="sfChat_MessageTapped" />
sfChat.MessageTapped += SfChat_MessageTapped;
private void sfChat_MessageTapped(object sender, MessageTappedEventArgs e)
{
DisplayAlert("Message", " Tapped on message :" + e.Message.Author.Name, "Ok");
}
MessageTapped Command
<sfChat:SfChat x:Name="sfChat"
MessageTappedCommand="{Binding TappedCommand}" />
public class ViewModel : INotifyPropertyChanged
{
public Command<object> tappedCommand;
public ViewModel()
{
// Assigning command action to ICommand type property
TappedCommand = new Command<object>(MessageTapped);
}
// ICommand type property for binding with sfChat.MessageTappedCommand
public Command<object> TappedCommand
{
get { return tappedCommand; }
set { tappedCommand = value; }
}
private void MessageTapped(object args)
{
var MessageTappedArgs = obj as MessageTappedEventArgs;
DisplayAlert("Message", "Tapped on Message :" + MessageTappedArgs.Message.Author.Name, "Ok");
}
}
MessageDoubleTapped Event and Command
The SfChat
control includes built-in features like the MessageDoubleTapped event and MessageDoubleTappedCommand. These are activated when a message is double-tapped. You can access the message that was double-tapped and the point of interaction through the MessageDoubleTappedEventArgs.
MessageDoubleTapped Event
<sfChat:SfChat x:Name="sfChat"
MessageDoubleTapped="sfChat_MessageDoubleTapped" />
sfChat.MessageDoubleTapped += SfChat_MessageDoubleTapped;
private void sfChat_MessageDoubleTapped(object? sender, MessageDoubleTappedEventArgs e)
{
DisplayAlert("Message", " DoubleTapped on message :" + e.Message.Author.Name, "Ok");
}
MessageDoubleTapped Command
<sfChat:SfChat x:Name="sfChat"
MessageDoubleTappedCommand="{Binding DoubleTappedCommand}" />
public class ViewModel : INotifyPropertyChanged
{
public Command<object> doubleTappedCommand;
public ViewModel()
{
// Assigning command action to ICommand type property
DoubleTappedCommand = new Command<object>(MessageDoubleTapped);
}
// ICommand type property for binding with sfChat.MessageDoubleTappedCommand
public Command<object> DoubleTappedCommand
{
get { return doubleTappedCommand; }
set { doubleTappedCommand = value; }
}
private void MessageDoubleTapped(object obj)
{
var MessageDoubleTappedArgs= obj as MessageDoubleTappedEventArgs;
DisplayAlert("Message", "DoubleTapped on Message :" + MessageDoubleTappedArgs.Message.Author.Name, "Ok");
}
}
MessageLongPressed Event and Command
The SfChat
control has built-in features like the MessageLongPressed event and MessageLongPressedCommand. These are activated when a message is long-pressed. You can access the message that was long-pressed and the point of interaction through the MessageLongPressedEventArgs.
MessageLongPressed Event
<sfChat:SfChat x:Name="sfChat"
MessageLongPressed="sfChat_MessageLongPressed" />
sfChat.MessageLongPressed += sfChat_MessageLongPressed;
private void sfChat_MessageLongPressed(object sender, MessageLongPressedEventArgs e)
{
DisplayAlert("Message", " LongPressed on message :" + e.Message.Author.Name, "Ok");
}
MessageLongPressed Command
<sfChat:SfChat x:Name="sfChat"
MessageLongPressedCommand ="{Binding LongPressedCommand }" />
public class ViewModel : INotifyPropertyChanged
{
public Command<object> longPressedCommand;
public ViewModel()
{
// Assigning command action to ICommand type property
LongPressedCommand = new Command<object>(MessageLongPressed);
}
// ICommand type property for binding with sfChat.MessageLongPressedCommand
public Command<object> LongPressedCommand
{
get { return longPressedCommand; }
set { longPressedCommand = value; }
}
private void MessageLongPressed(object obj)
{
var MessageLongPressedArgs = obj as MessageLongPressedEventArgs;
DisplayAlert("Message", "LongPressed on Message :" + MessageLongPressedArgs.Message.Author.Name, "Ok");
}
}