Xamarin.forms 形式:可扩展布局?

Xamarin.forms 形式:可扩展布局?,xamarin.forms,Xamarin.forms,我的任务是将现有的“静态”垂直布局转换为某种可扩展的布局。这是预期的结果 我知道XF框架没有这种现成的东西。问题是如何创建一个自定义控件来嵌入所有类型的其他控件。请注意,子控件可以是任何内容,包括另一个具有多个子控件的布局 在配置方面,我需要类似XF master detail页的内容。例如 <ThisNewControl Caption="Description"> <ThisNewControl.Content> <Entry Text="{Bi

我的任务是将现有的“静态”垂直布局转换为某种可扩展的布局。这是预期的结果


我知道XF框架没有这种现成的东西。问题是如何创建一个自定义控件来嵌入所有类型的其他控件。请注意,子控件可以是任何内容,包括另一个具有多个子控件的布局

在配置方面,我需要类似XF master detail页的内容。例如

<ThisNewControl Caption="Description">
  <ThisNewControl.Content>
     <Entry Text="{Binding Description}"/>
  <ThisNewControl.Content>
</ThisNewControl>

<ThisNewControl Caption="More Stuff">
  <ThisNewControl.Content>
     <StackLayout>
...
     </StackLayout>
  <ThisNewControl.Content>
</ThisNewControl>

...

以下是我的结局

using Xamarin.Forms;

namespace SmartwebsCrossPlatform.Portable.Controls {
    class ExpandableGrid : Grid {
        static readonly Thickness IconMargin = new Thickness(9, 0);

        View content;
        public View Content {
            get => content;
            set {
                content = value;
                Children.Add( value, 0, 1 );
            }
        }

        Label lblDesc = new Label();
        Label icon = new Label { Text = ">", Margin = IconMargin, HorizontalOptions = LayoutOptions.End };

        public string Caption {
            set {
                lblDesc.Text = value;
            }
        }

        public ExpandableGrid() {
            RowDefinitions = new RowDefinitionCollection {
                new RowDefinition { Height = GridLength.Auto },
                new RowDefinition { Height = 0 },
            };
            ColumnDefinitions = new ColumnDefinitionCollection {
                new ColumnDefinition { Width = GridLength.Star },
            };
            Children.Add( lblDesc, 0, 0 );
            Children.Add( icon, 0, 0 );
            lblDesc.GestureRecognizers.Add( new TapGestureRecognizer { Command = new Command( LabelClick ) } );
        }

        void LabelClick() {
            if( RowDefinitions[1].Height.IsAuto ) {
                RowDefinitions[1].Height = 0;
                icon.Text = ">";
            } else {
                RowDefinitions[1].Height = GridLength.Auto;
                icon.Text = "v";
            }
        }
    }
}
XAML使用:

<controls:ExpandableGrid Caption="Additional Details">
  <controls:ExpandableGrid.Content>
    <Entry Text="{Binding AdditionalDetail}"/>
  </controls:ExpandableGrid.Content>
</controls:ExpandableGrid>


Xamarin.Forms 4.6+引入了扩展元素,这要感谢社区成员Andrei Misiukevich。尽管它目前是实验性的(需要设置Expander_Experional标志),但我对它的体验非常积极。

“问题是如何创建一个自定义控件,以嵌入各种其他控件。”-在这种情况下,您不必指定嵌入式控件的类型。克拉克·肯特有这样的销售愿景;-)