Xamarin.forms 中的响应式网格

Responsive grid in Xamarin.forms

关于 ,我需要一些帮助来使列表视图项在所有平台、所有尺寸以及横向和纵向模式下响应。我现在拥有的列表视图在 10" 屏幕上变得很小。我想让它响应。任何参考来实现这个?

如果您的 ListView 很小并且没有覆盖整个页面,这听起来像是您遇到了某种固定宽度问题?

在您的 mainList 上,您设置的 ContentPage 没有任何布局选项。

首先尝试设置外部元素的 HorizontalOptionsVerticalOptions,一旦你开始工作,关注内部控件以确保它们占用所需数量的 space 之后。

        o.HorizontalOptions = LayoutOptions.FillAndExpand;
        o.VerticalOptions = LayoutOptions.FillAndExpand;

ListView BackgroundColor 更改为一些引人注目的颜色,以帮助您确保事物布局正确。

每个 Xamarin.Forms.Element(因此每个页面)实现宽度和高度 属性,您可以使用它们根据页面大小调整布局。 您应该在此页面上获取有关 VisualElement class https://developer.xamarin.com/api/type/Xamarin.Forms.VisualElement/

的战利品

您还可以使用 Device.Idiom 检查您使用的是 phone 还是平板电脑,并分别处理每个案例:

        if (Device.Idiom == TargetIdiom.Phone)
            //your code goes here
        else
            if (Device.Idiom == TargetIdiom.Tablet)
            //your code goes here
        else
            throw new PlatformNotSupportedException();

对于 landscape/protrait,只需检查您页面的 Width/Height-Properties:

        if (Width > Height)
        {
            //"landscape"
        }
        else
        {
            //"portrait"
        }

为了处理方向更改,为 SizeChanged 实施事件处理程序或简单地覆盖 OnSizeAllocated (https://developer.xamarin.com/api/member/Xamarin.Forms.VisualElement.OnSizeAllocated/p/System.Double/System.Double/)