When a message is sent from the server, over SignalR, ChatService will parse this message and transform it into a Message object. Then, it will raise an event called NewMessage, which is defined in ChatService.
In this section, we will implement an event handler to handle these events and add them to the Messages collection, unless a message with the same ID already exists.
Again, perform the following steps and look at the code to get to grips with it:
- In ChatViewModel, create a method called ChatService_NewMessage, which will be a standard event handler. This has two parameters: sender, which is of the object type, and e, which is of the Events.NewMessageEventArgs type.
- Wrap the code in this method in Device.BeginInvokeOnMainThread(). We're doing this since we are going to add messages to the Message collection. Items that are added to this collection will modify the view, and any code that modifies the...