How yo create Custom Shell Renderer in Microsoft MAUI

Bhuwan 721 Reputation points
2022-09-13T11:16:32.663+00:00

Hi

how to use below code in Microsoft MAUI same like Xamarin Forms

CustomShell.cs

namespace TestApp.Controls
{
/// <summary>
/// CustomShell
/// </summary>
public class CustomShell : Shell
{
}
}

CustomShellRenderer.cs inside platform

[assembly: ExportRenderer(typeof(CustomShell), typeof(CustomShellRenderer))]
namespace TestApp.Platforms.Android.Controls
{
public class CustomShellRenderer : ShellRenderer
{
/// <summary>
/// CustomShellRenderer
/// </summary>
/// <param name="context"></param>
public CustomShellRenderer(Context context) : base(context)
{

    }  

    /// <summary>  
    /// IShellItemRenderer  
    /// </summary>  
    /// <param name="shellItem"></param>  
    /// <returns></returns>  
    protected override IShellItemRenderer CreateShellItemRenderer(ShellItem shellItem)  
    {  
        return new CustomShellItemRenderer(this);  
    }  
}  

/// <summary>  
/// CustomShellItemRenderer  
/// </summary>  
public class CustomShellItemRenderer : ShellItemRenderer  
{  
    /// <summary>  
    /// TodoShellItemRenderer  
    /// </summary>  
    /// <param name="shellContext"></param>  
    public CustomShellItemRenderer(IShellContext shellContext) : base(shellContext)  
    {  

    }  

    /// <summary>  
    /// Pops to root when the selected tab is pressed.  
    /// </summary>  
    /// <param name="shellSection"></param>  
    protected override void OnTabReselected(ShellSection shellSection)  
    {  
        try  
        {  
            Device.BeginInvokeOnMainThread(async () =>  
            {  
                await shellSection?.Navigation.PopToRootAsync();  
            });  
        }  
        catch (Exception ex)  
        {  
            Debug.WriteLine(ex);  
        }  
    }  
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,542 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 31,976 Reputation points Microsoft Vendor
    2022-09-14T07:43:54.817+00:00

    Hello @Bhuwan ,

    According to your description, the CustomShell.cs has been moved outside the Platforms folder, and the platform-specific implementation file( CustomShellRenderer.cs ) has been moved to the corresponding Platform folder.

    ExportRenderer won't be needed in .NET MAU, you can remove it.

    //[assembly: ExportRenderer(typeof(CustomShell), typeof(CustomShellRenderer))]  
    

    Then you could configure renderers in the MauiProgram.cs

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

    For more details, please refer to the Wiki- Using Custom Renderers in .NET MAUI · dotnet/maui Wiki · GitHub ( Using handlers.AddCompatibilityRenderer will cause a crash, you have to use handlers.AddHandler )

    Best Regards,
    Wenyan Zhang


    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.