Xamarin.Forms

Code Examples Upgrade Guide User Guide Demos Support Forums Download
  • Code Examples
  • Upgrade Guide
  • User Guide
  • Demos
  • Support
  • Forums
  • Download
Class ChatMessageTemplateSelector

    Show / Hide Table of Contents

    Class ChatMessageTemplateSelector

    Provides override methods to load a custom template for messages based on its type.

    Inheritance
    System.Object
    ChatMessageTemplateSelector
    Namespace: Syncfusion.XForms.Chat
    Assembly: Syncfusion.SfChat.XForms.dll
    Syntax
    public class ChatMessageTemplateSelector : DataTemplateSelector
    Remarks

    Application developers override the Xamarin.Forms.DataTemplateSelector.OnSelectTemplate(System.Object,Xamarin.Forms.BindableObject) method to return a unique Xamarin.Forms.DataTemplate based on the message type. Call base.OnSelectTemplate(item, container) to load the default template of the message.

    Examples

    The following code example demonstrates how to load custom templates from sample level.

    using Syncfusion.XForms.Chat;
    using Xamarin.Forms;
    

    namespace GettingStarted { public partial class ChatPage : ContentPage { SfChat sfChat; GettingStartedViewModel viewModel; public ChatPage() { InitializeComponent(); sfChat = new SfChat(); viewModel = new GettingStartedViewModel(); this.sfChat.Messages = viewModel.Messages; this.sfChat.CurrentUser = viewModel.CurrentUser; this.sfChat.MessageTemplate = new MyCustomTemplateSelector(this.sfChat); this.Content = sfChat; } } }

    public class MyCustomTemplateSelector : ChatMessageTemplateSelector { private SfChat sfChat; public MyCustomTemplateSelector(SfChat chat) : base(chat) { this.sfChat = chat; } protected override DataTemplate OnSelectTemplate(object item, BindableObject container) { // The below codes loads a custom template view whenever a text message is loaded. if (item as ITextMessage != null) { return new DataTemplate(() => { var label = new Label() { TextColor = Color.Blue, Margin = 10, BackgroundColor = Color.Gold, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.End, Text = (item as ITextMessage).Text }; return label; }); } // The below code loads the default template for all the other message types. else { return base.OnSelectTemplate(item, container); } } }

    Constructors

    ChatMessageTemplateSelector(SfChat)

    Initializes a new instance of the ChatMessageTemplateSelector class.

    Declaration
    public ChatMessageTemplateSelector(SfChat chat)
    Parameters
    Type Name Description
    SfChat chat

    The instance of the SfChat control.

    Methods

    OnSelectTemplate(Object, BindableObject)

    Returns the required template based on the message type.

    Declaration
    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    Parameters
    Type Name Description
    System.Object item

    The actual message as System.Object.

    Xamarin.Forms.BindableObject container

    The actual row data as Xamarin.Forms.BindableObject.

    Returns
    Type Description
    Xamarin.Forms.DataTemplate

    A developer-defined Xamarin.Forms.DataTemplate that can be used to display a message.

    Back to top Generated by DocFX
    Copyright © 2001 - 2023 Syncfusion Inc. All Rights Reserved