Getting Started

This example will guide you through the steps needed to add a basic RadChat control in your application. For the purpose of the example a simple echo bot will be defined, which simply repeats the sent message.

Before you proceed, please, take a look at these articles and follow the instructions to setup your app:

Example

If your app is setup, you are ready to add a RadChat control to your page.

<telerikConversationalUI:RadChat x:Name="chat" />

In addition to this you need to add the following namespace:

xmlns:telerikConversationalUI="clr-namespace:Telerik.XamarinForms.ConversationalUI;assembly=Telerik.XamarinForms.ConversationalUI"

So, for a simple demonstration purpose a sample echo bot will be defined.

public class RepeatBotService
{
    private Action<string> onReceiveMessage;
    internal void AttachOnReceiveMessage(Action<string> onMessageReceived)
    {
        this.onReceiveMessage = onMessageReceived;
    }
    internal void SendToBot(string text)
    {
        Task.Delay(500).ContinueWith(t => this.onReceiveMessage?.Invoke(text));
    }
}

Then, initialize the RepeatBotService and subscribe to the CollectionChanged event of the Items collection of the Chat instance:

private RepeatBotService botService;
private Author botAuthor;

public ChatGettingStartedXaml()
{
    InitializeComponent();

    this.botService = new RepeatBotService();
    this.botService.AttachOnReceiveMessage(this.OnBotMessageReceived);
    this.botAuthor = new Author { Name = "botty"};

    ((INotifyCollectionChanged)this.chat.Items).CollectionChanged += ChatItems_CollectionChanged; ;
}

Finally, add the needed event handlers:

private void ChatItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.Action == NotifyCollectionChangedAction.Add)
    {
        TextMessage chatMessage = (TextMessage)e.NewItems[0];
        if (chatMessage.Author == chat.Author)
        {
            this.botService.SendToBot(chatMessage.Text);
        }
    }
}
private void OnBotMessageReceived(string message)
{
    Device.BeginInvokeOnMainThread(() =>
    {
        TextMessage textMessage = new TextMessage();
        textMessage.Data = message;
        textMessage.Author = this.botAuthor;
        textMessage.Text = message;
        chat.Items.Add(textMessage);
    });
}

Figure 1: RadChat Getting Started

Chat Getting Started

SDK Browser and QSF applications contain different examples that show RadChat's main features. You can find the applications in the Examples and QSF folders of your local Telerik UI for Xamarin installation.

See Also

Is this article helpful? Yes / No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy