Net Maui Custom shell renderer custom button and shell bottom navigation is not working ?

Sami 966 Reputation points
2023-10-19T10:14:21.7+00:00

I have custom renderer for bottom shell navigation on the middle button like that but is not working shell navigation like this.. how can I access it sa ys null referance

customshellrenderer ....




 internal class CustomShellItemRenderer : ShellItemRenderer
 {
     public CustomShellItemRenderer(IShellContext context) : base(context)
     {
     }

 public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
 {
     var view = base.OnCreateView(inflater, container, savedInstanceState);


 if (Context is not null && ShellItem is CustomTabBar { CenterViewVisible: true } tabbar)
 {
     var rootLayout = new FrameLayout(Context)
     {
         LayoutParameters = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent)
     };

  var middleView = new Button(Context)   {       LayoutParameters = middleViewLayoutParams          };            middleView.Click += delegate   {       tabbar.CenterViewCommand?.Execute(null);   };

   rootLayout.AddView(middleView);        
   return rootLayout;   

 }     
return view;

 }

 }

custom tabbar

  public partial class CustomTabBar : TabBar
{

  [AutoBindable]
  private ICommand? centerViewCommand;

  [AutoBindable]
  private ImageSource? centerViewImageSource;

  [AutoBindable]
  private string? centerViewText;

  [AutoBindable]
  private bool centerViewVisible;

  [AutoBindable]
  public Color? centerViewBackgroundColor;

  }

appshell.cs

  public AppShell()
  {

  InitializeComponent();
  BindingContext = this;

  Routing.RegisterRoute("plus", typeof(Plus));

}


   public ICommand CViewCommand { get; } = new Command(async () => await Shell.Current.GoToAsync($"//plus"));

appshell.xml

    <c:CustomTabBar  CenterViewVisible="True" CenterViewCommand="{Binding CViewCommand}">

        <Tab Title="Tab1">
            <Tab.Icon>
                <FontImageSource FontFamily="Fontello" Glyph="{StaticResource icon-explore}"/>
            </Tab.Icon>
            <ShellContent Shell.TabBarBackgroundColor="{StaticResource DarkBack}" Shell.TabBarTitleColor="{StaticResource White}" Shell.TabBarUnselectedColor="{StaticResource Gray300}"
		Title="Page1"
		ContentTemplate="{DataTemplate vw:Explore}"
		Route="explore" />
        </Tab>
        <Tab Title="Tab2">
            <Tab.Icon>
                <FontImageSource FontFamily="Fontello" Glyph="{StaticResource icon-category}"/>
            </Tab.Icon>
                <ShellContent Shell.TabBarBackgroundColor="{DynamicResource BackColor}" Shell.TabBarTitleColor="{DynamicResource MainTextColor}" Shell.TabBarUnselectedColor="{StaticResource Gray300}"
		Title="Page2"
		ContentTemplate="{DataTemplate vw:Categories}"
		Route="categories" />
        </Tab>
        <Tab Title="Tab3">
            <ShellContent Shell.TabBarBackgroundColor="{DynamicResource BackColor}" Shell.TabBarTitleColor="{DynamicResource MainTextColor}" Shell.TabBarUnselectedColor="{StaticResource Gray300}"
	Title="Page1"
	ContentTemplate="{DataTemplate vw:Plus}"
    Route="plus"/>
        </Tab>
        <Tab Title="Tab4">
            <Tab.Icon>
                <FontImageSource FontFamily="Fontello" Glyph="{StaticResource icon-favorites}"/>
            </Tab.Icon>
            <ShellContent Shell.TabBarBackgroundColor="{DynamicResource BackColor}" Shell.TabBarTitleColor="{DynamicResource MainTextColor}" Shell.TabBarUnselectedColor="{StaticResource Gray300}"
	Title="Page1"
	ContentTemplate="{DataTemplate vw:Favorites}"
	Route="favorites" />
        </Tab>
        <Tab Title="Tab5">
            <Tab.Icon>
                <FontImageSource FontFamily="Fontello" Glyph="{StaticResource icon-user}"/>
            </Tab.Icon>
            <ShellContent Shell.TabBarBackgroundColor="{DynamicResource BackColor}" Shell.TabBarTitleColor="{DynamicResource MainTextColor}" Shell.TabBarUnselectedColor="{StaticResource Gray300}"
	Title="Page2"
	ContentTemplate="{DataTemplate vw:Account}"
	Route="account" />
        </Tab>

Developer technologies .NET .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-10-20T05:22:59.93+00:00

    Hello,

    You do not add the view to the rootLayout by rootLayout.AddView(view);, please add it.

    Here is my tested code in OnCreateView method.

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
         var view = base. OnCreateView(inflater, container, savedInstanceState);
    
        if (Context is not null && ShellItem is CustomTabBar { CenterViewVisible: true } tabbar)
         {
             var rootLayout = new FrameLayout(Context)
             {
                 LayoutParameters = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent)
             };
             rootLayout.AddView(view);
             const int middleViewSize = 150;
             var middleViewLayoutParams = new FrameLayout.LayoutParams(
                 ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent,
                 GravityFlags.CenterHorizontal | GravityFlags.Bottom)
             {
                 BottomMargin = 100,
                 Width = middleViewSize,
                 Height = middleViewSize
             };
             var middleView = new Button(Context) { LayoutParameters = middleViewLayoutParams }; middleView.Click += delegate { tabbar. CenterViewCommand?. Execute(null); };
    
            rootLayout.AddView(middleView);
             return rootLayout;
    
        }
         return view;
    }
    

    By the way, please do not forget to register this custom renderer in the CreateMauiApp method of MauiProgram.cs

       
    builder
     .ConfigureMauiHandlers(handlers =>
    
                     {
    #if ANDROID
                         handlers.AddHandler(typeof(Shell), typeof(yourProject.Platforms.Android.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.


0 additional answers

Sort by: Most helpful

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.