Coder Social home page Coder Social logo

maui-chat-control's Introduction

Use DevExpress .NET MAUI Components to Build a Chat View

This example uses DevExpress .NET MAUI Components to display a chat view with sender and receiver messages, suggested quick replies, and response input controls.

DevExpress Chat for .NET MAUI

Included Controls and Their Properties

Implementation Details

  1. Use a SafeKeyboardAreaView as the root for the view layout. This way, the device keyboard will not overlap the message view when it appears.

    <dx:SafeKeyboardAreaView> 
        ... 
    </dx:SafeKeyboardAreaView>
  2. Use the DXCollectionView control to display messages. Specify the data source and item templates (use different templates for sender and receiver). Groups items using a built-in algorithm that uses date/time ranges ("Today", "Yesterday", "Last Week", and so on). Specify a template for group headers.

        <dxcv:DXCollectionView
        ItemsSource="{Binding Messages}"
        ItemTemplate="{local:MessageTemplateSelector SenderTemplate=..., RecipientTemplate=...}"
        GroupHeaderTemplate="{StaticResource dayGroupTemplate}"
        ...
        >
            <dxcv:DXCollectionView.GroupDescription>
                <dxcv:GroupDescription FieldName="SentAt" GroupInterval="DateRange" />
            </dxcv:DXCollectionView.GroupDescription>
        </dxcv:DXCollectionView>
  3. Use DXContentPresenter components to define templates for sender and receiver messages.

        <ContentPage.Resources>
                <DataTemplate x:Key="senderMessageTemplate"  x:DataType="{x:Type local:Message}">
                    <dx:DXContentPresenter ... >
                    </dx:DXContentPresenter>
                </DataTemplate>
                <DataTemplate x:Key="recipientMessageTemplate" x:DataType="{x:Type local:Message}">
                    <dx:DXContentPresenter ... >
                    </dx:DXContentPresenter>
                </DataTemplate>
                ...
        </ContentPage.Resources>
  4. Call the DXCollectionView.ScrollTo() method to scroll the view to the last message:

    public partial class MainPage : ContentPage {
        // ...
        void OnMessagesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
            chatSurface.ScrollTo(chatSurface.GetItemHandle(vm.Messages.Count - 1));
        }
    }
  5. Use the ChipGroup control to display short answers:

    <dxe:ChipGroup ...      
        DisplayMember="Text"
        ItemsSource="{Binding SuggestedActions}" />
    public ChatViewModel() {
        // ...
        SuggestedActions = new ObservableCollection<SuggestedAction>() {
            new SuggestedAction() { Message = new Message() { Sender = Me, SentAt = DateTime.Now, Text = "Sure" }, Text = "Sure" },
            new SuggestedAction() { Message = new Message() { Sender = Me, SentAt = DateTime.Now, Text = "Great" }, Text = "Great" },
            new SuggestedAction() { Message = new Message() { Sender = Me, SentAt = DateTime.Now, Text = "Thank you" }, Text = "Thank you" },
            new SuggestedAction() { Message = new Message() { Sender = Me, SentAt = DateTime.Now, Text = "My pleasure" }, Text = "My pleasure" }
        };
    }
  6. Use the TextEdit control to create an input field where a user can text a message. To send the message, specify the DXButton control.

    <Grid ... >
        <dxe:TextEdit ...
            x:Name="messageEditor"
            Text="{Binding EditMessageText}"/>
        <dx:DXButton ... 
            Command="{Binding SendMessageCommand}"
            CommandParameter="{Binding EditMessageText}"/>
    </Grid>

Files to Review

Documentation

More Examples

maui-chat-control's People

Contributors

sergepilipchuk avatar

Stargazers

 avatar HuaFangYun avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.