|
<ContentPage.Resources>
<ResourceDictionary>
<local:MessageConverter x:Key="MessageConverter" />
<local:TimestampFormatConverter x:Key="TimestampFormatConverter" />
<local:TimestampVisibilityConverter x:Key="TimestampVisibilityConverter" />
<local:AuthorNameVisibilityConverter x:Key="AuthorNameVisibilityConverter" />
<local:MessageDeliverStatusConverter x:Key="MessageDeliverStatusConverter" />
<local:MessageDeliverStatusBadgeIconConverter x:Key="MessageDeliverStatusBadgeIconConverter" />
<Style TargetType="sfChat:OutgoingMessageAuthorView">
<Setter Property="ControlTemplate">
<Setter.Value>
<ControlTemplate>
<Label x:Name="authorNameLabel"
Text="{Binding Author.Name}"
LineBreakMode="TailTruncation"
TextColor="#D0E8FF"
VerticalTextAlignment="Center"
BindingContext="{TemplateBinding BindingContext}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="sfChat:OutgoingMessageTextView">
<Setter Property="ControlTemplate">
<Setter.Value>
<ControlTemplate>
<Label x:Name="messageLabel"
Text="{Binding Text}" TextColor="#FFFFFF"
VerticalTextAlignment="Center" BindingContext="{TemplateBinding BindingContext}"
FontAttributes="{TemplateBinding BindingContext,Converter={StaticResource MessageDeliverStatusConverter}}"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="sfChat:OutgoingMessageTimestampView">
<Setter Property="ControlTemplate">
<Setter.Value>
<ControlTemplate>
<Grid ColumnSpacing="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" x:Name="timestampLabel" TextColor="#D0E8FF"
Text="{TemplateBinding BindingContext, Converter={StaticResource TimestampFormatConverter}}" />
<Image Grid.Column="1" HeightRequest="5" WidthRequest="5" Source="{TemplateBinding BindingContext, Converter={StaticResource MessageDeliverStatusBadgeIconConverter}}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<sfChat:SfChat x:Name="sfChat"
MessageShape="RoundedRectangle"
ShowOutgoingMessageAvatar="True"
ShowIncomingMessageAvatar="True"
ItemsSource="{Binding MessageCollection}"
ItemsSourceConverter="{StaticResource MessageConverter}"
CurrentUser="{Binding CurrentUser}"
IncomingMessageTimestampFormat="hh:mm tt"
OutgoingMessageTimestampFormat="hh:mm tt" >
</sfChat:SfChat>
</ContentPage.Content>
Message model.
public class MessageModel : INotifyPropertyChanged
{
private bool isDeliveried;
private string text;
public Author User { get; set; }
public bool IsDeliveried
{
get => isDeliveried;
set
{
isDeliveried = value;
RaisePropertyChanged("IsDeliveried");
}
}
public string Text
{
get => text;
set
{
text = value;
RaisePropertyChanged("Text");
}
}
}
ViewModel collection generation.
this.messageCollection.Add(new MessageModel()
{
User = CurrentUser,
IsDeliveried = true,
Text = "Hi guys, good morning! I'm very delighted to share with you the news that our team is going to launch a new mobile application.",
}) ;
this.messageCollection.Add(new MessageModel()
{
User = new Author() { Name = "Andrea", Avatar = "People_Circle7.png" },
Text = "Oh! That's great.",
});
this.messageCollection.Add(new MessageModel()
{
User = new Author() { Name = "Harrison", Avatar = "People_Circle5.png" },
Text = "That is good news.",
});
this.messageCollection.Add(new MessageModel()
{
User = CurrentUser,
Text = "Hello, Andrea and Harrison",
});
Converter code.
internal class MessageDeliverStatusConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
var message = value as ITextMessage;
var originalMessage = message.Data as MessageModel;
if(originalMessage.IsDeliveried)
{
return FontAttributes.Bold;
}
}
return FontAttributes.Italic;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return FontAttributes.None;
}
} |