简体   繁体   English

Windows Phone获取网格的高度和宽度

[英]Windows Phone get height and width of grid

I try to get get height and width of grid: 我尝试获取网格的高度和宽度:

int numberOfColumnsInGameView = (int)(gameView.Width / objectSize);
int numberOfRowsInGameView = (int)(gameView.Height / objectSize -0.5);

Where gameView is grid. 其中gameView是网格。 When I try assign to variable I have that var = NaN 当我尝试分配给变量时,我有var = NaN

I suggest you to use Grid.ActualHeight and Grid.ActualWidth properties. 我建议您使用Grid.ActualHeightGrid.ActualWidth属性。 You should use Height and Width only if you need to set a new value. 仅在需要设置新值时才应使用“ Height和“ Width

The most conservative way is probably to do something like this: 最保守的方法可能是执行以下操作:

private double HeightOf(FrameworkElement element)
{
    if (double.IsNaN(element.Height))
        return element.ActualHeight;
    if (double.IsNaN(element.ActualHeight))
        return element.Height;
    return Math.Max(element.Height, element.ActualHeight);
}

This way, you cover the following cases: 这样,您可以涵盖以下情况:

  • Element is rendered, but no explicit height is set (→ ActualHeight ) 呈现元素,但未设置任何明确的高度(→ ActualHeight
  • Element is not rendered yet, but has an explicit height (→ Height ) 元素尚未渲染,但是具有显式的高度(→ Height
  • Neither one (will return 0) 都不返回(将返回0)

Thx I understood. 我明白了。

this.Loaded += MainPage_Loaded; 
.........
private void GeneranteLevel(int level)
    {
int numberOfColumnsInGameView = (int)(gameView.ActualWidth / objectSize);
int numberOfRowsInGameView = (int)(gameView.ActualHeight / objectSize -0.5);
........

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM