Getting the sent message (MVVM)

Hi,

Im trying to use SfChat on my app, im switching to MVVM (im kinda new to the concept), and i have set up everything, the messages are fetched from the server and everything is working; now i created a command to send new messages but im struggling with finding the message i wanna send.
My question is, how do i get the entered message and pass it to my ViewModel when the SendMessageCommand is executed ?

Note: I Tried to set a breakpoint when my Command is executed so i can see if the new message has been added to my ObservableCollection<object> Messages, but it doesnt seem to be happening, although, when my command is done executing, the message is added to the SfChat.

Note: for now, im using the regular SendMessage event and passing + executing my ViewModel's sendCommand from code behind, which kinda breaks the MVVM pattern

Thank you

3 Replies 1 reply marked as answer

KK Karthikraja Kalaimani Syncfusion Team September 2, 2020 06:01 AM UTC

Hi Fodil,

We suspect that you have not created the Instance for SendMessage Command on constructor of ViewModel class. So, only the Execute method is not triggered. We have attached the working sample for your reference.
 
 
Code snippet :

 
<sfChat:SfChat x:Name="sfChat"  
                           SendMessageCommand="{Binding SendMessageCommand}" 
                           Messages="{Binding Messages}"  
                           CurrentUser="{Binding CurrentUser}" > 
            </sfChat:SfChat>

//ViewModel
  private SendMessageCommandExt sendMessageCommand; 
 
        /// <summary> 
        /// Gets or sets the message conversation. 
        /// </summary> 
        public SendMessageCommandExt SendMessageCommand 
        { 
            get 
            { 
                return this.sendMessageCommand; 
            } 
            set 
            { 
                this.sendMessageCommand = value; 
                RaisePropertyChanged("SendMessageCommand"); 
            } 
        }
public ImageMessageViewModel() 
        { 
            this.messages = new ObservableCollection<object>(); 
            this.currentUser = new Author() { Name = "Nancy", Avatar = "People_Circle16.png" }; 
            SendMessageCommand = new SendMessageCommandExt(); 
            this.GenerateMessages(); 
        } 
……
public class SendMessageCommandExt : ICommand 
    { 
        public event EventHandler CanExecuteChanged; 
 
        public bool CanExecute(object parameter) 
        { 
            return true; 
        } 
 
        public void Execute(object parameter) 
        { 
            (parameter as SendMessageEventArgs).Handled = false; 
        } 
    } 


Please check the sample and let us know if you still facing the same issue? If not, please modify the sample based on your scenario and revert us back.

Sample link : https://www.syncfusion.com/downloads/support/directtrac/general/ze/SfChat-1827353421.zip

 

Regards,
Karthik Raja
 


Marked as answer

FO Fodil September 2, 2020 09:52 AM UTC

It worked! thank you for the response and the example



KK Karthikraja Kalaimani Syncfusion Team September 3, 2020 06:24 AM UTC

  
Hi Fodil,

Thanks for the update.
We are glad to know that your requirement has been achieved at your end. Please let us know if you have any further queries on this. We are happy to help you. 

Regards,
Karthik Raja
 


Loader.
Up arrow icon