MasterDetailPage in Xamarin.Forms App

2020-04-17 08:10发布

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

enter image description here

2条回答
放我归山
2楼-- · 2020-04-17 08:41

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);
查看更多
狗以群分
3楼-- · 2020-04-17 08:57

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

enter image description here

查看更多
登录 后发表回答