|
1 | 1 | # How-to-create-a-contact-list-with-initials-using-.NET-MAUI-Avatar-View |
2 | 2 | This repository contains sample which demonstrates how to create a contact list with initials using .NET MAUI Avatar View |
| 3 | + |
| 4 | +The .NET Multi-platform App UI (MAUI) Avatar View (Initials View) provides a graphical representation of the user image that allows you to customize the view by adding image, background color, icon, text, and more. |
| 5 | +The .NET MAUI Initials View can be achieved by using the `ContentType` property which should be set to `Initials`. |
| 6 | + |
| 7 | +## Type of .NET MAUI Initials View |
| 8 | +There are two types of .NET MAUI Initials View available in the SfAvatarView: |
| 9 | +* Single character initial view. |
| 10 | +* Double character initial view. |
| 11 | +### .NET MAUI Single Character Initial View |
| 12 | +The .NET MAUI single character initial view can be achieved using the `SingleCharacter` option of the `InitialsType` property of the SfAvatarView. |
| 13 | +The following code sample is an example for .NET MAUI Initials View with single character. |
| 14 | + |
| 15 | +**XAML** |
| 16 | + |
| 17 | +<sfAvatar:SfAvatarView ContentType="Initials" |
| 18 | + InitialsType="SingleCharacter" > |
| 19 | +</sfAvatar:SfAvatarView> |
| 20 | + |
| 21 | +### .NET MAUI Double Character Initials View |
| 22 | +The .NET MAUI double character initial view can be achieved using the `DoubleCharacter` option of the `InitialsType` property of the SfAvatarView. |
| 23 | +The following code sample is an example for .NET MAUI Initials View with double characters. |
| 24 | + |
| 25 | +**XAML** |
| 26 | + |
| 27 | +<sfAvatar:SfAvatarView ContentType="Initials" |
| 28 | + InitialsType="DoubleCharacter" > |
| 29 | +</sfAvatar:SfAvatarView> |
| 30 | + |
| 31 | +## Steps to add .NET MAUI Initials View |
| 32 | +This section explains how to create a contact list with initials view with a double character in a .NET MAUI application. |
| 33 | +**Step 1**: Create a .NET MAUI application project in Visual Studio 2022. |
| 34 | +**Step 2**: Add the [Syncfusion.Maui.Core](https://www.nuget.org/packages/Syncfusion.Maui.Core/) Nuget to the project from [nuget.org](https://www.nuget.org/). |
| 35 | +**Step 3**: In the MauiProgram.cs file, register the Syncfusion.Maui.Core handler as follows. |
| 36 | + |
| 37 | +**C#** |
| 38 | + |
| 39 | +using Syncfusion.Maui.Core.Hosting; |
| 40 | +namespace AvatarView_ContactList; |
| 41 | + |
| 42 | +public static class MauiProgram |
| 43 | +{ |
| 44 | + public static MauiApp CreateMauiApp() |
| 45 | + { |
| 46 | + var builder = MauiApp.CreateBuilder(); |
| 47 | + builder |
| 48 | + .UseMauiApp<App>() |
| 49 | + .ConfigureSyncfusionCore() |
| 50 | + .ConfigureFonts(fonts => |
| 51 | + { |
| 52 | + fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); |
| 53 | + }); |
| 54 | + |
| 55 | + return builder.Build(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +**Step 4**: Add the .NET MAUI Avatar View control namespace to the MainPage.xaml. |
| 61 | + |
| 62 | +**XAML** |
| 63 | + |
| 64 | +<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" |
| 65 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 66 | + x:Class="AvatarView_ContactList.MainPage" |
| 67 | + xmlns:sfAvatar="clr-namespace:Syncfusion.Maui.Core;assembly=Syncfusion.Maui.Core" |
| 68 | + xmlns:local="clr-namespace:AvatarView_ContactList"> |
| 69 | + |
| 70 | + |
| 71 | +**Step 5**: Create a list of contact details in the view model. Have a property Name that holds the value to be shown as in the .NET MAUI Initials View. |
| 72 | + |
| 73 | +**C#** |
| 74 | + |
| 75 | +public class Contact |
| 76 | +{ |
| 77 | + public string Name { get; set; } |
| 78 | +} |
| 79 | + |
| 80 | +public class ContactsViewModel |
| 81 | +{ |
| 82 | + public List<Contact> ListOfContacts { get; set; } |
| 83 | + |
| 84 | + public ContactsViewModel() |
| 85 | + { |
| 86 | + ListOfContacts = new List<Contact>(); |
| 87 | + |
| 88 | + ListOfContacts.Add(new Contact() { Name = "Tucker Davis" }); |
| 89 | + ListOfContacts.Add(new Contact() { Name = "Elizabeth Ann" }); |
| 90 | + ListOfContacts.Add(new Contact() { Name = "Lily Sue" }); |
| 91 | + ListOfContacts.Add(new Contact() { Name = "Mary Margaret" }); |
| 92 | + ListOfContacts.Add(new Contact() { Name = "Sophia Grace" }); |
| 93 | + ListOfContacts.Add(new Contact() { Name = "Andrew James" }); |
| 94 | + ListOfContacts.Add(new Contact() { Name = "George Frances" }); |
| 95 | + ListOfContacts.Add(new Contact() { Name = "James Richard" }); |
| 96 | + ListOfContacts.Add(new Contact() { Name = "John Peter" }); |
| 97 | + ListOfContacts.Add(new Contact() { Name = "Logan James" }); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +**Step 6**: Define the Avatar View (SfAvatarView) in the XAML and set the `ContentType` as `Initials` and set the `InitialType` property as `DoubleCharacter`. To create initials for the contact in the .NET MAUI Avatar View, populate the contact list in the collection view and bind the contact’s name to the `AvatarName` property. |
| 103 | +This will display the Avatar View as Initials View in a circle shape. |
| 104 | + |
| 105 | +**XAML** |
| 106 | + |
| 107 | +<ContentPage.BindingContext> |
| 108 | + <local:ContactsViewModel/> |
| 109 | +</ContentPage.BindingContext> |
| 110 | + |
| 111 | +<Grid> |
| 112 | + <CollectionView ItemsSource="{Binding ListOfContacts}"> |
| 113 | + <CollectionView.ItemTemplate> |
| 114 | + <DataTemplate> |
| 115 | + <HorizontalStackLayout Spacing="10" Margin="10"> |
| 116 | + <sfAvatar:SfAvatarView ContentType="Initials" |
| 117 | + InitialsType="DoubleCharacter" |
| 118 | + AvatarName="{Binding Name}" |
| 119 | + WidthRequest="50" |
| 120 | + AvatarColorMode="LightBackground" |
| 121 | + HeightRequest="50" |
| 122 | + CornerRadius="25"> |
| 123 | + </sfAvatar:SfAvatarView> |
| 124 | + <Label Text="{Binding Name}" |
| 125 | + HorizontalOptions="Center" VerticalOptions="Center" |
| 126 | + HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/> |
| 127 | + </HorizontalStackLayout> |
| 128 | + </DataTemplate> |
| 129 | + </CollectionView.ItemTemplate> |
| 130 | + </CollectionView> |
| 131 | +</Grid> |
| 132 | + |
| 133 | + |
| 134 | +You can explore the runnable demo of achieving the .NET MAUI Initials View in this repository |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
0 commit comments