【问题标题】:Get the size of hidden view - Xamarin.Forms获取隐藏视图的大小 - Xamarin.Forms
【发布时间】:2017-10-23 10:14:07
【问题描述】:

我有一个空的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,则YellowGridWidth 等于-1(这也是合乎逻辑的)。但是如果隐藏,是否可以获得所需的网格大小?

更新

接下来我尝试了:

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

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

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    您可以使用Xamarin.Forms.VisualElement.Measure 方法确定控件的大小请求。

    【讨论】:

      【解决方案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>
      

      【讨论】:

        猜你喜欢
        • 2017-05-14
        • 1970-01-01
        • 2012-06-22
        • 1970-01-01
        • 2014-01-22
        • 2016-08-14
        • 1970-01-01
        • 2020-02-08
        相关资源
        最近更新 更多