获取隐藏视图的大小 - Xamarin.Forms

3
我有一个空的ContentPage,里面包含一个Grid
    <Grid
        x:Name="YellowGrid"
        BackgroundColor="Yellow">
        <Label 
            Text="It's a Yellow Grid"
            VerticalOptions="Center"
            HorizontalOptions="Center"
            />
    </Grid>

在重写的方法中,我可以获得网格的实际大小:
    protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);
        // YellowGrid.Width equals to the width of the screen
    }

没关系。但是如果我将网格属性IsVisible=false设置为true,那么YellowGridWidth将等于-1(这也是合理的)。但是如果它被隐藏了,是否可以获取所需网格的大小呢?

更新

我尝试了下面的方法:

    protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height);
        var size = YellowGrid.Measure(width, height, MeasureFlags.None);
    }

但是MeasureYellowGrid返回的是标签Label所需的大小,而不是整个网格将放置的大小。

2个回答

3

2
我用 Opacity=0InputTransparent=true 替代 IsVisible=false 来达到隐藏网格的相同效果。
<Grid
    x:Name="YellowGrid"
    Opacity="0"
    InputTransparent="True"
    BackgroundColor="Yellow">
    <Label 
        Text="It's a Yellow Grid"
        VerticalOptions="Center"
        HorizontalOptions="Center"
        />
</Grid>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接