MasterDetailPage in Xamarin.Forms App

大城市里の小女人 提交于 2019-12-02 07:42:27

问题


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



来源:https://stackoverflow.com/questions/48384691/masterdetailpage-in-xamarin-forms-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!