ScreenWidth错误值或ListView WidthRequest奇怪的行为

时间:2018-02-21 10:09:34

标签: xaml listview xamarin xamarin.forms

我试图在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);

我的结果的截图

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

enter image description here

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

WidthRequestHeightRequest使用的是相同的值但帧数比它们的宽度要高,所以我很确定WidthAndHeightForDashboard的计算值高于它应该的值...或者屏幕上App.ScreenWidth所声明的空间可能更少?

1 个答案:

答案 0 :(得分:0)

我仍然不知道究竟发生了什么,但我设法找到了一种解决方法(或正确的解决方案来做到这一点?我不知道):

我现在不是设置WidthRequest的{​​{1}}和HeightRequest,而是为Frame设置Padding,我计算如下:

Frame

我将用这张图片解释计算:

enter image description here

  

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

     

3 * IconWidthAndHeight =我有3个小工具,大小为46x46

     

除以6,得到填充的值

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