2017-05-23 47 views
1

我有一個空ContentPageGrid內:獲取隱藏視圖的大小 - Xamarin.Forms

<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(這也是合乎邏輯的)。但是如果它隱藏起來,是否有可能獲得所需的網格大小?

UPDATE

我明年受審:

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

MeasureLabel,而不是全尺寸格柵將放置YellowGrid返回所需的大小。

回答

0

我用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>