CustomShellRenderer to change the color of navbar to a linear gradient

Muhammad Faizan 1 Reputation point
2022-12-22T11:01:53.837+00:00

I am trying to change the color of the navbar background to a linear gradient.
As I am using an Appshell, I have a CustomShellRenderer to change the navbar background color.

I have tried this custom renderer on xamarin forms project which works fine.
But on .net maui its not change the background color of navbar. I am posting my custom renderer down too.

`using Android.Content;  
using Microsoft.Maui.Controls.Handlers.Compatibility;  
using Microsoft.Maui.Controls.Platform.Compatibility;  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace Custodian.Platforms.Android.Renderers  
{  
    public class CustomShellRenderer : ShellRenderer  
    {  
        public CustomShellRenderer(Context context) : base(context)  
        {  
        }  
  
        protected override IShellToolbarAppearanceTracker CreateToolbarAppearanceTracker()  
        {  
            return new CustomToolbarAppearanceTracker();  
        }  
    }  
}  
public class CustomToolbarAppearanceTracker : IShellToolbarAppearanceTracker  
{  
    public void Dispose()  
    {  
  
    }  
  
    public void ResetAppearance(AndroidX.AppCompat.Widget.Toolbar toolbar, IShellToolbarTracker toolbarTracker)  
    {  
  
    }  
  
    public void SetAppearance(AndroidX.AppCompat.Widget.Toolbar toolbar, IShellToolbarTracker toolbarTracker, ShellAppearance appearance)  
    {  
        toolbar.SetBackgroundResource(Custodian.Resource.Drawable.navbar_gradient);  
    }  
}`  

on .net maui project it never hit the line 38
toolbar.SetBackgroundResource(Custodian.Resource.Drawable.navbar_gradient);

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,008 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 80,771 Reputation points Microsoft External Staff
    2022-12-23T03:14:08.02+00:00

    Hello,

    You need configure renderers in the MauiProgram.cs

       builder  
                   .UseMauiApp<App>()  
                           .ConfigureMauiHandlers(handlers => {  
                           #if ANDROID  
                               handlers.AddHandler(typeof(Shell), typeof(Custodian.Platforms.Android.Renderers.CustomShellRenderer ));  
                           #endif  
                           })  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.