A Microsoft framework for building cross-platform mobile apps using .NET and C# with native performance and user interfaces.
Hello,
Welcome to our Microsoft Q&A platform!
For this function, you could use the DataTemplateSelector to consume a different style for the incoming message and outgoing message.
Check the code:
<ContentPage.Resources>
<ResourceDictionary>
<local:CustomDataTemplateSelector x:Key="MessageTemplateSelector"/>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<ListView ...
ItemTemplate="{StaticResource MessageTemplateSelector}" />
...
</StackLayout>
</ContentPage.Content>
Model class
public class MessageModel
{
private bool isIncoming;
public bool IsIncoming
{
get => isIncoming;
set
{
isIncoming = value;
NotifyPropertyChanged(nameof());
}
}
...
}
Custom message DataTemplateSelector class.
public class CustomDataTemplateSelector : DataTemplateSelector
{
private readonly DataTemplate incomingDataTemplate;
private readonly DataTemplate outgoingDataTemplate;
public MyDataTemplateSelector()
{
this.incomingDataTemplate = new DataTemplate(typeof(IncomingViewCell));
this.outgoingDataTemplate = new DataTemplate(typeof(OutgoingViewCell));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var messageVm = item as MessageModel;
if (messageVm == null)
return null;
return messageVm.IsIncoming ? this.incomingDataTemplate : this.outgoingDataTemplate;
}
}
You could refer to this link: https://github.com/jamesmontemagno/app-monkeychat
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.