简体   繁体   中英

How to disable Master page in MasterDetailPage of Xamarin Forms

I am working with Xamarin forms and I need to disable the Master page that I use as context menu depending on whether user is logged in or not. I have both Master and Detail pages as separate XAML pages.

  <MasterDetailPage.Master>
    <view:MenuPage/>
  </MasterDetailPage.Master>

  <MasterDetailPage.Detail>
    <view:MainViewPage 
      x:Name="MainView"/>
  </MasterDetailPage.Detail> 

As you might have guessed, I am trying to incorporate MVVM here, so I tried binding visibility ( IsVisible ) and enabled ( IsEnabled ) properties of the Master page, however, I still get the undesired black fade effect when pushing navigation button to access my menu. Instead, I need to completely eat up the button press action.

Should your pages be visible whenever the user is connected or not ? Or do you have a login page at the start of the application for example ?

If you don't have pages that are visible by both connected users or not, you could implement the login page or another page by defining it as ContentPage. It will take all the screen space and hide the navigationBar. Then after user connect you call a page as MasterDetailPage and then you will have your navigationBar, ...

Don't know if that's what you're looking for but i hope i was able to help you.

This can be achieved with a custom NavigationRenderer, by overriding the Click event of the drawer icon with your custom logic.

[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationPageRenderer))]
namespace RTW.Mobile.App.Droid.Renderers
{
    public class CustomNavigationPageRenderer : NavigationPageRenderer, IMessageSender
    {
        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);

            var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            for (var i = 0; i < toolbar.ChildCount; i++)
            {
                var imageButton = toolbar.GetChildAt(i) as ImageButton;
                var drawerArrow = imageButton?.Drawable as DrawerArrowDrawable;

                if (drawerArrow == null)
                    continue;

                //ensure only one handler is registered
                imageButton.Click -= imageButton_Click;
                imageButton.Click += imageButton_Click;
            }
        }

        private void imageButton_Click(object sender, EventArgs e)
        {
            if (!App.IsBlockingConditionTrue)
            {
                MessagingCenter.Send<IMessageSender>(this, "ToggleMasterIsPresented");
            }
        }
    }
}

Then just subscribe to the message with MessagingCenter.Subscribe<IMessageSender>(this, "ToggleMasterIsPresented", OnToggleMasterIsPresented); and handle it.

    private void OnToggleMasterIsPresented(IMessageSender obj)
    {
        _masterDetailPage.IsPresented = !_masterDetailPage.IsPresented;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM