【问题标题】:ScreenWidth wrong value or ListView WidthRequest strange behaviourScreenWidth 错误值或 ListView WidthRequest 奇怪行为
【发布时间】:2018-02-21 10:09:33
【问题描述】:

我试图在FlowListView 中每行放置三个二次帧。


xaml:

FlowListView代码:

<toolkit:FlowListView 
    FlowColumnCount="3"
    HasUnevenRows="True"
    FlowRowBackgroundColor="{Binding WidgetMainColor}"
    Margin="10,0,10,0"
    FlowItemsSource="{Binding TestContainer.WidgetsInContainer}">
    <toolkit:FlowListView.FlowColumnTemplate>
        <DataTemplate>
            <Frame
                Margin="5"
                WidthRequest="{Binding WidthAndHeightForDashboard}"
                HeightRequest="{Binding WidthAndHeightForDashboard}"
                BackgroundColor="{Binding WidgetMainColor}"
                CornerRadius="2"
                HasShadow="False"/>
        </DataTemplate>
    </toolkit:FlowListView.FlowColumnTemplate>
</toolkit:FlowListView>

计算框架的宽度/高度:

WidthAndHeightForDashboard:

public double WidthAndHeightForDashboard
{
    get
    {
        // -50 for Margin (6 * 5 for Frame margins, 2 * 10 for FlowListView margin)
        // rest is divided by 3 because we need 3 frames
        return (double)(App.ScreenWidth - 50) / 3; 
    }
}

我使用ScreenWidth 来计算WidthAndHeightForDashboard,因为我知道页面将始终处于纵向模式。这意味着我们在此页面上的高度将大于宽度,因此宽度对于二次框架来说是决定性的。

ScreenWidth 是这样计算的:

App.ScreenWidth = (int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);

我的结果的屏幕截图

正如您在屏幕截图中看到的那样,帧的高度大于宽度,但它们应该是二次的:

我还尝试删除所有边距并从计算中删除边距,以防我的计算出现错误,但这并不能解决问题。

WidthRequestHeightRequest 使用相同的值,但帧的高度大于宽度,所以我很确定 WidthAndHeightForDashboard 的计算值高于应有的值...或者可能存在App.ScreenWidth 所说的屏幕上的空间是否更少?

【问题讨论】:

  • App.ScreenWidth 是否给出了正确的行为?移动平台上有多种尺寸概念,如果出现偏差,这可能是您问题的根源。加载后查看FlowListView 的宽度。

标签: xaml listview xamarin xamarin.forms


【解决方案1】:

我仍然不知道到底发生了什么,但我设法找到了解决方法(或正确的解决方案?我不知道):

我现在没有设置FrameWidthRequestHeightRequest,而是为Frame 设置Padding,我这样计算:

return (double)(App.ScreenWidth - 30 - 3 * IconWidthAndHeight) / 6;

我会用这张图来解释计算:

30 = 当前行的宽度边距汇总 (5 + 10 + 10 + 5)

3 * IconWidthAndHeight = 我有 3 个大小为 46x46 的小部件

除以 6 得到填充的值

抱歉,我知道这并不能完全回答原始问题,因为我没有在问题中包含图片。但也许这对那里的人有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多