我有一個空ContentPage
與Grid
內:獲取隱藏視圖的大小 - 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
,那麼YellowGrid
的Width
等於-1
(這也是合乎邏輯的)。但是如果它隱藏起來,是否有可能獲得所需的網格大小?
UPDATE
我明年受審:
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
var size = YellowGrid.Measure(width, height, MeasureFlags.None);
}
但Measure
爲Label
,而不是全尺寸格柵將放置YellowGrid
返回所需的大小。