旋转手机屏幕时,如何使垂直(XAML)切换到另一个设计的水平(XAML),而不是自动将旧的垂直(XAML)布局到水平(XAML)
1 回答
Assuming you currently use a StackPanel
(or similar), and have already specified SupportedOrientations
as PortraitOrLandscape
, you need to design your page constnet in a Grid
instead, and then trap the OrientationChanged
event and write code to reposition the UI elemnts in the grid.
There is a Quickstart Tutorial on MSDN that shows the technique.
EDIT
You could change to different XAML by trapping the OrientationChanged
event and displaying a different page using Frame.Navigate
, but I think that would result in some duplicate code. Or, you can code your page so that you just have to move items around in the Grid
like the MSDN exmaple code shows:
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
// Switch the placement of the buttons based on an orientation change.
if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
{
Grid.SetRow(buttonList, 1);
Grid.SetColumn(buttonList, 0);
}
// If not in portrait, move buttonList content to visible row and column.
else
{
Grid.SetRow(buttonList, 0);
Grid.SetColumn(buttonList, 1);
}
}
Alternative is to navigate to a different page - so in your portrait page, you might have:
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
// Switch to a different page based on an orientation change.
if ((e.Orientation & PageOrientation.Landscape) == (PageOrientation.Landscape))
{
// switch to landscape orientation page
NavigationService.Navigate(new Uri("/LandscapePage.xaml", UriKind.Relative));
}
}
Then you would have similar code in the landscape page, to detect the orientation change and navigate back to the portrait page.