event when the left menu is opened in xamarin forms shell

Viewed 1066

I am working with xamarin forms shell. I have 1 image and 1 label to assign the name and profile picture of the user. I want every time the menu opens (press the button or drag to the right) to update that value again, but I can't find any events related to it. Someone please help me badly

2 Answers

Since there is no and won't be events such OnFlyoutOpened OnFlyoutClosed, you can listen to your Shell PropertyChanged event, if the property is FlyoutIsPresented then execute your code:

public AppShell()
{
    InitializeComponent();
    PropertyChanged += Shell_PropertyChanged;
}

private void Shell_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName.Equals("FlyoutIsPresented"))
        if (FlyoutIsPresented)
            OnFlyoutOpened();      //you will execute your code here
        else
            OnFlyoutClosed();
}

Depending on your requirement you will define OnFlyoutOpened() and OnFlyoutClosed() methods.

Thanks to @PureWeen guidance in discussion.

You could use custom renderer to get the event when you press the shell hamburger icon.

[assembly: ExportRenderer(typeof(AppShell), typeof(ShellCustomRenderer))]

namespace ShellDemo.Droid
{
public class ShellCustomRenderer : ShellRenderer
{
    public ShellCustomRenderer(Context context) : base(context)
    {

    }

    protected override IShellToolbarAppearanceTracker CreateToolbarAppearanceTracker()
    {
        return new ToolbarAppearance();
    }
}

public class ToolbarAppearance : IShellToolbarAppearanceTracker
{
    public void Dispose()
    {

    }

    public void ResetAppearance(Android.Support.V7.Widget.Toolbar toolbar, IShellToolbarTracker toolbarTracker)
    {
        toolbar.NavigationClick += Toolbar_NavigationClick1;
    }

    private void Toolbar_NavigationClick1(object sender, Android.Support.V7.Widget.Toolbar.NavigationClickEventArgs e)
    {
       //this event would be fired when the hamburger icon clicked.
    }

    private void Toolbar_NavigationClick(object sender, Android.Support.V7.Widget.Toolbar.NavigationClickEventArgs e)
    {
        
    }

    public void SetAppearance(Android.Support.V7.Widget.Toolbar toolbar, IShellToolbarTracker toolbarTracker, ShellAppearance appearance)
    {
      
    }
}

}
Related