Getting Started with .NET MAUI Badge View (SfBadgeView)

9 Mar 20225 minutes to read

This section explains the steps required to configure the .NET MAUI Badge View control for .NET MAUI Badge Notifications and customize its elements.

Adding a SfBadgeView reference

The Syncfusion .NET MAUI controls are available in Nuget.org. To add SfBadgeView to your project, open the NuGet package manager in Visual Studio, search for Syncfusion.Maui.Core and then install it.

Handler registration

In the MauiProgram.cs file, register the handler for Syncfusion core.

  • C#
  • using Microsoft.Maui;
        using Microsoft.Maui.Hosting;
        using Microsoft.Maui.Controls.Compatibility;
        using Microsoft.Maui.Controls.Hosting;
        using Microsoft.Maui.Controls.Xaml;
        
    `using Syncfusion.Maui.Core.Hosting;`
    
        namespace EffectsViewMauiSample
        {
          public static class MauiProgram
          {
            public static MauiApp CreateMauiApp()
            {
                var builder = MauiApp.CreateBuilder();
                builder
                .UseMauiApp<App>()
     .`ConfigureSyncfusionCore()`
     
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                });
    
                return builder.Build();
             }
           
          }
    
        }

    Adding a namespace

    Add the following namespace to add .NET MAUI Badge Notifications.

    xmlns:badge="clr-namespace:Syncfusion.Maui.Core;assembly=Syncfusion.Maui.Core"
    using Syncfusion.Maui.Core;

    Initializing a badge view

    Create an instance for the Badge View control, and add it as content.

    <badge:SfBadgeView >        
    </badge:SfBadgeView>
    //Initializing the badge view.
    
    SfBadgeView sfBadgeView = new SfBadgeView();
    
    this.Content = sfBadgeView;

    Adding a badge notification text

    Add text to Badge View using the BadgeText property.

    <badge:SfBadgeView>        
        <badge:SfBadgeView BadgeText="20" />          
    </badge:SfBadgeView>
    SfBadgeView sfBadgeView = new SfBadgeView();
    
    //Adding text to the badge view.
    
    sfBadgeView.BadgeText = "20";
    
    this.Content = sfBadgeView;

    Adding a content

    An Image, button, or label or any view can be added to the Badge View using the Content property.

    <badge:SfBadgeView HorizontalOptions="Center" VerticalOptions="Center" >
            <badge:SfBadgeView.Content>
                <Button Text="Primary" WidthRequest="120"  HeightRequest="60"/>
            </badge:SfBadgeView.Content>
    </badge:SfBadgeView>
    SfBadgeView sfBadgeView = new SfBadgeView();
    sfBadgeView.HorizontalOptions = LayoutOptions.Center;
    sfBadgeView.VerticalOptions = LayoutOptions.Center;
    //Adding image to the content of the badge view.
    Button button = new Button();
    button.Text = "Primary";
    button.WidthRequest = 120;
    button.HeightRequest = 60;
    sfBadgeView.Content = button;
    Content = sfBadgeView;

    The following code sample gives you the complete code for Badge View with badge notification types and text.

    <badge:SfBadgeView HorizontalOptions="Center" VerticalOptions="Center" BadgeText="20">
            <badge:SfBadgeView.Content>
                <Button Text="Primary" WidthRequest="120"  HeightRequest="60"/>
            </badge:SfBadgeView.Content>
    </badge:SfBadgeView>
    public MainPage()
            {
                InitializeComponent();
                SfBadgeView sfBadgeView = new SfBadgeView();
                sfBadgeView.HorizontalOptions = LayoutOptions.Center;
                sfBadgeView.VerticalOptions = LayoutOptions.Center;
                sfBadgeView.BadgeText = "20";
    
                //Adding image to the content of the badge view.
                Button button = new Button();
                button.Text = "Primary";
                button.WidthRequest = 120;
                button.HeightRequest = 60;
                sfBadgeView.Content = button;
                Content = sfBadgeView;
            }

    The following screenshot illustrates the result of the above code.

    .NET MAUI Badge View Getting Started