【问题标题】:Change width of Master of MasterDetailPage for android in xamarin-forms在 xamarin-forms 中更改 android 的 MasterDetailPage 大师的宽度
【发布时间】:2017-11-02 03:34:28
【问题描述】:

在 xamarin-forms 应用程序中, 我想为安卓应用更改左侧菜单的宽度。

我正在尝试按照以下代码更改宽度

class MyMasterDetailPage : MasterDetailPage
{
    public static readonly BindableProperty DrawerWidthProperty = BindableProperty.Create<MyMasterDetailPage, int>(p => p.DrawerWidth, default(int));

    public int DrawerWidth
    {
        get { return (int)GetValue(DrawerWidthProperty); }
        set { SetValue(DrawerWidthProperty, value); }
    }
    }

现在是安卓应用代码。

[assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.MasterDetailPage), typeof(MyMasterDetailRenderer))]
class MyMasterDetailRenderer : MasterDetailRenderer
{
    bool firstDone;

    public override void AddView(View child)
    {
        if (firstDone)
        {
            MyMasterDetailPage page = (MyMasterDetailPage)this.Element;
            LayoutParams p = (LayoutParams)child.LayoutParameters;
            p.Width = page.DrawerWidth;
            base.AddView(child, p);
        }
        else
        {
            firstDone = true;
            base.AddView(child);
        }
    }        
}

上面的代码不工作。 请分享有关如何更改 Android ap 的 MasterDetailPage 宽度的想法?

【问题讨论】:

    标签: c# xamarin.android xamarin.forms


    【解决方案1】:

    你提供的代码确实有效,问题是this.Element现在没有暴露,通常我们可以通过覆盖OnElementChanged方法得到MyMasterDetailPage的实例,问题是这里AddView被调用早于OnElementChanged.

    目前我找不到一种优雅的方式来在MyMasterDetailPage 中定义BindableProperty 并在AddView 事件中获取此属性的值,但我们可以直接在渲染器的代码中设置它,例如:

    if (firstDone)
    {
        LayoutParams p = (LayoutParams)child.LayoutParameters;
        p.Width = 300;
        base.AddView(child, p);
    }
    else
    {
        firstDone = true;
        base.AddView(child);
    }
    

    如果我能在这里找到更好的解决方案,我会回来更新这个答案。

    【讨论】:

    • p.Width = 300 不起作用。如果应用任何静态宽度并且在应用程序中不受影响
    • @user2960398,你能告诉我你的xf版本吗?它在我身边工作,只是pcl中定义的属性不起作用。
    • 这适用于移动设备,但不适用于平板电脑
    • 这里有更好的解决方案:stackoverflow.com/questions/48384691/…
    【解决方案2】:
    [assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.MasterDetailPage), typeof(MyMasterDetailRenderer))]
    class MyMasterDetailRenderer : MasterDetailRenderer
    {
        bool firstDone;
    
        public override void AddView(View child)
        {
            if (firstDone)
            {
                var visualElementRenderer = this as IVisualElementRenderer;
                var element = visualElementRenderer.Element;
                MyMasterDetailPage page = (MyMasterDetailPage)element;
                LayoutParams p = (LayoutParams)child.LayoutParameters;
                p.Width = page.DrawerWidth;
                base.AddView(child, p);
            }
            else
            {
                firstDone = true;
                base.AddView(child);
            }
        }        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      相关资源
      最近更新 更多