且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Xamarin.Forms应用程序中的MasterDetailPage

更新时间:2023-02-26 21:09:26

我搜索一个小菜单.修改大小宽度为MasterPage

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

您可以在MasterDetailPageRenderer中更改Master页面的宽度.

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

首先,在自定义MasterDetailPage类中创建一个BindableProperty:

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); }
    }
}

第二,在您的MasterDetailPageRenderer中,像这样设置MyMasterDetailPage宽度:

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;
    }
}

用法,在我的App.xaml.cs中,我将页面设置如下:

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;
}

更新:

关于Xamarin.iOS,您可以参考 ,受 P3PPP的项目的启发,您唯一需要做的就是更改此行 MyPhoneMasterDetailRenderer.cs :

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);