Xamarin.Forms应用程序中的MasterDetailPage

时间:2018-01-22 15:12:04

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

是否可以在void set_method(const char *method) { // Check if the method is serial_port if (strcmp(method, "serial_port") == 0) { // assign the alias "print" to serial_print // something like defing here a function like this: // print(const char *print) { serial_print(print); } print(const char *print) = serial_print(const char *message) } else if (strcmp(method, "graphical_tty") == 0) { // The same that serial_port case but with graphical_tty_print } else { // Error } } 中创建类似的侧边菜单?

enter image description here

2 个答案:

答案 0 :(得分:2)

  

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

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

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

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

答案 1 :(得分:0)

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

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

Slide Over Kit details here

enter image description here