|
1 | 1 | # assign-unique-color-to-each-user-name-and-message-in-a-group-chat-in-Xamarin.Forms.Chat |
2 | 2 | Describes how to assign a separate color to each user in a group chat to display that user's name and that user's message in one unique color. |
| 3 | + |
| 4 | +## Sample |
| 5 | + |
| 6 | +```xaml |
| 7 | + |
| 8 | + <ContentPage.Resources> |
| 9 | + <ResourceDictionary> |
| 10 | + <local:AuthorColorConverter x:Name="authorColorConverter" x:Key="authorColorConverter" /> |
| 11 | + |
| 12 | + <!--Defining outgoing message author color--> |
| 13 | + <Style TargetType="sfChat:OutgoingMessageAuthorView"> |
| 14 | + <Setter Property="ControlTemplate"> |
| 15 | + <Setter.Value> |
| 16 | + <ControlTemplate> |
| 17 | + <Label Text="{Binding Author.Name}" BackgroundColor="Transparent" TextColor="{TemplateBinding BindingContext, Converter= {StaticResource authorColorConverter},ConverterParameter={x:Reference viewModel}}" BindingContext="{TemplateBinding BindingContext}"/> |
| 18 | + </ControlTemplate> |
| 19 | + </Setter.Value> |
| 20 | + </Setter> |
| 21 | + </Style> |
| 22 | + |
| 23 | + <!--Defining incoming message author color--> |
| 24 | + <Style TargetType="sfChat:IncomingMessageAuthorView"> |
| 25 | + <Setter Property="ControlTemplate"> |
| 26 | + <Setter.Value> |
| 27 | + <ControlTemplate> |
| 28 | + <Label Text="{Binding Author.Name}" BackgroundColor="Transparent" TextColor="{TemplateBinding BindingContext, Converter= {StaticResource authorColorConverter},ConverterParameter={x:Reference viewModel}}" BindingContext="{TemplateBinding BindingContext}"/> |
| 29 | + </ControlTemplate> |
| 30 | + </Setter.Value> |
| 31 | + </Setter> |
| 32 | + </Style> |
| 33 | + </ResourceDictionary> |
| 34 | + </ContentPage.Resources> |
| 35 | + |
| 36 | +Converter: |
| 37 | + |
| 38 | + public class AuthorColorConverter : IValueConverter |
| 39 | + { |
| 40 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 41 | + { |
| 42 | + var viewModel = parameter as ViewModel; |
| 43 | + var message = value as IMessage; |
| 44 | + |
| 45 | + if (viewModel.AuthorColors.ContainsKey(message.Author)) |
| 46 | + { |
| 47 | + Color color; |
| 48 | + viewModel.AuthorColors.TryGetValue(message.Author, out color); |
| 49 | + return color; |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + return viewModel.AddColorForAuthor(message.Author); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 58 | + { |
| 59 | + return Color.White; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | +``` |
| 64 | + |
| 65 | +## Requirements to run the demo |
| 66 | + |
| 67 | +To run the demo, refer to [System Requirements for Xamarin](https://help.syncfusion.com/xamarin/system-requirements) |
| 68 | + |
| 69 | +## Troubleshooting |
| 70 | + |
| 71 | +### Path too long exception |
| 72 | + |
| 73 | +If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project. |
0 commit comments