【问题标题】:MasterDetailPage in Xamarin.Forms AppXamarin.Forms 应用程序中的 MasterDetailPage
【发布时间】:2018-07-01 06:25:32
【问题描述】:

是否可以在Xamarin.Forms 中创建类似的侧边菜单?

【问题讨论】:

  • 主详情页面的左侧只是一个Vertical StackLayout,所以你可以随意设计
  • 谢谢@OrcusZ,@Himanshu Dwivedi,但这不是我要找的,我已经看过这个解决方案但不是他的,我搜索了一个小菜单。将大小宽度修改为 MasterPage

标签: xamarin.forms xamarin.android master-pages master-detail


【解决方案1】:

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

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

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

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 宽度:

[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,我设置页面是这样的:

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,你可以参考this,灵感来自P3PPP's project,你唯一需要做的就是在MyPhoneMasterDetailRenderer.cs中更改这一行:

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

【讨论】:

  • 无法在 Xamarin Forms 5 中使用此解决方案
【解决方案2】:

如果您不想使用内置的主/细节滑出页面,因为它太宽且无法轻松调整大小,您可以使用滑过套件。

它是一个免费的开源组件,应该可以让你做你需要的。

Slide Over Kit details here

【讨论】:

    猜你喜欢
    • 2018-06-22
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    相关资源
    最近更新 更多