MasterDetailPage in Xamarin.Forms App

前端 未结 2 1275
时光说笑
时光说笑 2021-01-24 20:49

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

相关标签:
2条回答
  • 2021-01-24 21:11

    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);
    
    0 讨论(0)
  • 2021-01-24 21:31

    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

    0 讨论(0)
提交回复
热议问题