How to change the MasterDetailPage.MasterBehavior depending on the width

Hello,

I write a Xamarin forms application for UWP and try to use an adaptive UI. On a small screen I want the MasterBehavior to be 'popover' and on a large screen it should be 'split'. I registered the SizeChanged event and change the MasterBehaviour but it does not work. What must I do to make it work?

public MainPage()
        {
            this.InitializeComponent();
            SizeChanged += OnSizeChanged;
        }

        private void OnSizeChanged(object sender, EventArgs e)
        {
            if ( Width < 700)
            {
                this.MasterBehavior = MasterBehavior.Popover;
                UpdateChildrenLayout();
            } else
            {
                this.MasterBehavior = MasterBehavior.Split;
                UpdateChildrenLayout();
            }
        }

Posts

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    on iPad vs iPhone, this is already the default behavior. Are you sure you have to change anything to make it work on UWP?

  • Matthew.4307Matthew.4307 USMember ✭✭✭

    On Android, iOS and Windows RT you'll get this as long as the MasterBehaviour is set to Default. It also means you get pop-over in Portrait and generally get split in landscape. If that's not happening on UWP it's probably a Xamarin bug you'll need to report.

  • WafflesWaffles USMember ✭✭
    edited December 2016

    Try putting a break point on that sizechanged event and see if it fires. Im not sure that it will reach it because I think that might be more for changing to landscape, etc

  • SHeidrichSHeidrich DEMember

    Hello,

    @Waffles said:
    Try putting a break point on that sizechanged event and see if it fires. Im not sure that it will reach it because I think that might be more for changing to landscape, etc

    I've put some debug output into that method and see that the sizechanged event is fired.

    @JulienRosen said:
    on iPad vs iPhone, this is already the default behavior. Are you sure you have to change anything to make it work on UWP?

    @Matthew.4307 said:
    On Android, iOS and Windows RT you'll get this as long as the MasterBehaviour is set to Default. It also means you get pop-over in Portrait and generally get split in landscape. If that's not happening on UWP it's probably a Xamarin bug you'll need to report.

    You both are right. I've checked it. If the application is started in Portrait the behaviour is popover and in landscape it is split.

    But what doesn't work is changing the behaviour during runtime. I got this error in the console window while changing the behaviour from popover to split by making the application windows wider.

    Error: Cannot save value from target back to source. BindingExpression: Path='IsPaneOpen' DataItem='Xamarin.Forms.Platform.UWP.MasterDetailControl'; target element is 'Windows.UI.Xaml.Controls.SplitView' (Name='SplitView'); target property is 'IsPaneOpen' (type 'Boolean').
    
  • ChristophFinkChristophFink USMember

    @SHeidrich Did you find a solution for this? I am facing the same problem...

  • MSiccDevMSiccDev CHMember ✭✭
    Same here... I'll guess that needs a custom renderer...
Sign In or Register to comment.