简体   繁体   English

如何在 WinRT 中进行动态网格布局?

[英]How to make dynamic grid layout in WinRT?

I want a grid in my page to have a single column in portrait mode and have two columns in Landscape mode.我希望页面中的网格在纵向模式下只有一列,在横向模式下有两列。 Some of the content should move to the second column when the device is Landscape.当设备为横向时,某些内容应移至第二列。 I've tried to do this but couldn't.我试过这样做,但不能。

I'm doing everything from code behind.我正在从代码背后做所有事情。 Generating the grid, adding children etc. When Orientation changes, I destroy the current layout and create a new one.生成网格,添加子项等。当方向改变时,我会破坏当前布局并创建一个新布局。 The problem with this approach is, any entered data will be gone.这种方法的问题是,任何输入的数据都将消失。 It's a huge code and not possible to put here.这是一个巨大的代码,不可能放在这里。 I want this layout change to happen automatically.我希望这种布局更改自动发生。 So, any data that is entered is preserved after the orientation change.因此,任何输入的数据都会在方向更改后保留。

Any help would be appreciated.任何帮助,将不胜感激。

The trick is to have one grid that accommodates both states.诀窍是让一个网格同时容纳两种状态。 You can use VisualStates to assign different Grid.Row, Grid.Column, Grid.RowSpan and Grid.ColumnSpan values to controls.您可以使用 VisualStates 将不同的 Grid.Row、Grid.Column、Grid.RowSpan 和 Grid.ColumnSpan 值分配给控件。 This is done using Blend and is declarative in XAML.这是使用 Blend 完成的,并且在 XAML 中是声明性的。 you give each state a name.你给每个州一个名字。 In the code you detect size change of the window to trigger the different states using the VisualStateManager.在代码中,您使用 VisualStateManager 检测窗口大小的变化以触发不同的状态。

You can also do this without VisualStates.您也可以在没有 VisualStates 的情况下执行此操作。 In that case in the event handler of the SizeChanged you have to set the appropriate values for all the controls yourself.在这种情况下,在 SizeChanged 的​​事件处理程序中,您必须自己为所有控件设置适当的值。

In Windows 10 UWP it got much simpler using RelativePanel.在 Windows 10 UWP 中,使用 RelativePanel 变得更加简单。 You can position controls in a more flexible way then with grids.与网格相比,您可以以更灵活的方式定位控件。

Martin马丁

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

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