Window Phone 7 设备的方向
先看看WP的默认创建页面:
WP7默认创建一个横向或者竖式的显示页面。关于页面的显示有两个属性:
SupportedOrientations="Portrait" Orientation="Portrait" (竖式)
具体看看:
SupportedOrientations有三个可选项
- Portrait (the default)
- Landscape
- PortraitOrLandscape
Orientation有
- Landscape
- LandscapeLeft (tip the phone left)
- LandscapeRight (tip the phone right)
- Portrait
- PortraitDown (normal vertical position)
- PortraitUp (tip the phone upside-down)
同时WP还提供了一个事件:OrientationChanged 来监视设备方向的改变。
从上面的信息不难得出:要使页面适应设备的方向,可以设置SupportedOrientation为PortraitOrLandscape,在OrientationChanged事件设置Orientation来调整显示效果:如图
代码:
代码
<phone:PhoneApplicationPage
x:Class="WindowsPhoneApp.LandScape"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="PortraitOrLandscape" Orientation="Landscape"
mc:Ignorable="d" d:DesignHeight="480" d:DesignWidth="728"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Land Scape Test" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="112,83,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace WindowsPhoneApp
{
public partial class LandScape : PhoneApplicationPage
{
public LandScape()
{
InitializeComponent();
this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);
}
void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if (e.Orientation == PageOrientation.LandscapeLeft || e.Orientation == PageOrientation.LandscapeRight)
{
this.Orientation = PageOrientation.Portrait;
}
if (e.Orientation == PageOrientation.PortraitDown || e.Orientation == PageOrientation.PortraitUp)
{
this.Orientation = PageOrientation.Landscape;
}
}
}
}