MasterDetailPage in Xamarin.Forms App

2020-04-17 08:50发布

问题:

Is it possible to create a similar side menu in Xamarin.Forms ?

回答1:

I search a small menu. modify a size width to MasterPage

You could change the width of the Master page in MasterDetailPageRenderer.

First, create a BindableProperty in your custom MasterDetailPage class:

public class MyMasterDetailPage : MasterDetailPage
{
    public static readonly BindableProperty DrawerWidthProperty =
          BindableProperty.Create(
              "WidthRatio",
              typeof(int),
              typeof(MyMasterDetailPage),
              (float)0.2,
              propertyChanged: (bindable, oldValue, newValue) =>
              {
              });

    public float WidthRatio
    {
        get { return (float)GetValue(WidthRatioProperty); }
        set { SetValue(WidthRatioProperty, value); }
    }
}

Second, in your MasterDetailPageRenderer, set the MyMasterDetailPage width like this:

[assembly: ExportRenderer(typeof(MyMasterDetailPage), typeof(MyMasterDetailPageRenderer))]
...
public class MyMasterDetailPageRenderer : MasterDetailPageRenderer
{
    protected override void OnElementChanged(VisualElement oldElement, VisualElement newElement)
    {
        base.OnElementChanged(oldElement, newElement);

        var width = Resources.DisplayMetrics.WidthPixels;

        var fieldInfo = GetType().BaseType.GetField("_masterLayout", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        var _masterLayout = (ViewGroup)fieldInfo.GetValue(this);
        var lp = new DrawerLayout.LayoutParams(_masterLayout.LayoutParameters);

        MyMasterDetailPage page = (MyMasterDetailPage)newElement;
        lp.Width = (int)(page.WidthRatio * width);

        lp.Gravity = (int)GravityFlags.Left;
        _masterLayout.LayoutParameters = lp;
    }
}

Usage, in my App.xaml.cs, I set the page like this:

public App()
{
    InitializeComponent();

    var mdp = new MyMasterDetailPage
    {
        Master = new MenuPage() { Title = "Master" },
        Detail = new ContentPage
        {
            BackgroundColor = Color.White,
            Title = "DetailPage",
            Content = new Label
            {
                Text = "DetailPage",
                HorizontalTextAlignment = TextAlignment.Center,
                VerticalTextAlignment = TextAlignment.Center,
            },
        },
        WidthRatio = 0.5f,
    };
    MainPage = mdp;
}

Update:

About Xamarin.iOS, you could refer to this, inspired by P3PPP's project, the only thing you need do is change this line in MyPhoneMasterDetailRenderer.cs:

//Change 0.8 to whatever you want
masterFrame.Width = (int)(Math.Min(masterFrame.Width, masterFrame.Height) * 0.8);


回答2:

If you don't want to use the built in master/detail slide out page, as it is too wide and cannot be resized easily, you could probably use the slide over kit.

It is a free open source component that should allow you to do what you need.

Slide Over Kit details here