网格布局方法将 UI 元素放置在 Grid 中。当发生方向更改时,采用编程方式重新将元素放置在 Grid 的不同单元格中。
要使用网格布局方法,通常您会执行以下步骤。
-
将页面的 SupportedOrientations 属性更改为 PortraitOrLandscape。
-
将 Grid 用于内容面板。
-
创建一个 OrientationChanged 事件处理程序并添加代码以重新将元素放置在 Grid
如下代码
private void PhoneApplicationPage_OrientationChanged_1(object sender, OrientationChangedEventArgs e)
{
if ((e.Orientation & PageOrientation.Portrait) == PageOrientation.Portrait)//纵向
{
Grid.SetRow(buttonList, 1);
Grid.SetColumn(buttonList, 0);
}
else //横向
{
Grid.SetRow(buttonList, 0);
Grid.SetColumn(buttonList, 1);
}
}
如下图: