Windows Phone开发(6):处理屏幕方向的改变
原文:Windows Phone开发(6):处理屏幕方向的改变
俺们都知道,智能手机可以通过旋转手机来改变屏幕的显示方向,更多的时候,对于屏幕方向的改变,我们要做出相应的处理,例如,当手机屏幕方向从纵向变为横向时,可能要重新排列页面上的控件以适应显示区域的变化。
要使页面支持旋转,要把PhoneApplicationPage的SupportedOrientations属性改为PortraitOrLandscape,然后可以通过定义OrientationChanged事件来处理布局。形如:
<phone:PhoneApplicationPage ..............
SupportedOrientations="PortraitOrLandscape"
Orientation="Portrait"
OrientationChanged="PhoneApplicationPage_OrientationChanged">
一、Grid控件的处理。
<phone:PhoneApplicationPage
x:Class="Sample_PageDir.Page1"
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}"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"
SupportedOrientations="PortraitOrLandscape"
Orientation="Portrait"
OrientationChanged="PhoneApplicationPage_OrientationChanged"> <Grid x:Name="layoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image x:Name="img" Source="http://gubapic.eastmoney.com/member/e68/e681999/e68199920091216131540.jpg" Stretch="UniformToFill" Width="270" Grid.Row="0" Grid.Column="0" />
<TextBlock x:Name="txtBlock"
Grid.Row="1"
Grid.Column="0"
FontSize="70"
Margin="28">
<Run Foreground="Coral">信春哥</Run>
<LineBreak/>
<Run Foreground="Yellow">唱情歌</Run>
<LineBreak/>
<Run Foreground="SkyBlue">不挂科</Run>
</TextBlock>
</Grid> </phone:PhoneApplicationPage>
页面主要有两个控件,一个用于显示图片,一个用于显示文本信息,通过事件处理代码来相应改变两个控件的布局。
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
// 如果是横向的
if (e.Orientation == PageOrientation.Landscape ||
e.Orientation == PageOrientation.LandscapeLeft ||
e.Orientation == PageOrientation.LandscapeRight)
{
Grid.SetColumn(this.img, 0);
Grid.SetRow(this.img, 0);
Grid.SetRow(this.txtBlock, 0);
Grid.SetColumn(this.txtBlock, 1);
}
// 如果是纵向
else if (e.Orientation == PageOrientation.Portrait ||
e.Orientation == PageOrientation.PortraitDown ||
e.Orientation == PageOrientation.PortraitUp)
{
Grid.SetColumn(this.img, 0);
Grid.SetRow(this.img, 0);
Grid.SetRow(this.txtBlock, 1);
Grid.SetColumn(this.txtBlock, 0);
}
else
{
Grid.SetColumn(this.img, 0);
Grid.SetRow(this.img, 0);
Grid.SetRow(this.txtBlock, 1);
Grid.SetColumn(this.txtBlock, 0);
}
}
按F5运行程序,默认的屏幕方向是纵向的,如下图所示:
好,现在,我们把屏幕旋转一下,看看会怎么样。
二、StackPanel控件的处理。
<phone:PhoneApplicationPage
x:Class="Sample_PageDir.Page2"
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"
OrientationChanged="PhoneApplicationPage_OrientationChanged"
Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <phone:PhoneApplicationPage.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="46"/>
</Style>
</phone:PhoneApplicationPage.Resources> <StackPanel x:Name="pl">
<TextBlock Text="文本一"/>
<TextBlock Text="文本二"/>
<TextBlock Text="文本三"/>
</StackPanel>
</phone:PhoneApplicationPage>
后台事件处理代码。
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if (e.Orientation == PageOrientation.Landscape ||
e.Orientation == PageOrientation.LandscapeLeft ||
e.Orientation == PageOrientation.LandscapeRight)
{
this.pl.Orientation = System.Windows.Controls.Orientation.Horizontal;
}
else
{
this.pl.Orientation = System.Windows.Controls.Orientation.Vertical;
}
}
运行,默认方向是纵向。
把屏幕旋转后。
三、Canvas控件的处理。
<phone:PhoneApplicationPage
x:Class="Sample_PageDir.Page3"
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="Portrait"
OrientationChanged="PhoneApplicationPage_OrientationChanged"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Canvas x:Name="cv">
<Rectangle x:Name="rect1"
Width="232"
Height="238"
Fill="Red"
Canvas.Left="88"
Canvas.Top="88"/>
<Rectangle x:Name="rect2"
Height="192"
Width="275"
Fill="Yellow"
Canvas.Top="268"
Canvas.Left="155"/>
</Canvas> </phone:PhoneApplicationPage>
后台代码。后台代码。
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if (e.Orientation== PageOrientation.Landscape||e.Orientation== PageOrientation.LandscapeLeft||e.Orientation== PageOrientation.LandscapeRight)
{
Canvas.SetTop(this.rect1, 37);
Canvas.SetLeft(this.rect1, 46);
Canvas.SetTop(this.rect2, 240);
Canvas.SetLeft(this.rect2, 462);
}
else
{
Canvas.SetTop(this.rect1, 88);
Canvas.SetLeft(this.rect1, 88);
Canvas.SetTop(this.rect2, 268);
Canvas.SetLeft(this.rect2, 155);
}
}
看看效果。看看效果。
纵向。
横向。
Windows Phone开发(6):处理屏幕方向的改变的更多相关文章
- Android开发之改动屏幕方向
有的场景下.我们须要把手机屏幕方向改变,以下是我写的一个样例. xml页面文件: <RelativeLayout xmlns:android="http://schemas.andro ...
- Silverlight for Windows Phone开发系列课程
Silverlight for Windows Phone开发系列课程(1):Windows Phone平台概况 课程简介:本节开始介绍系列课程的概况,包括课程内容,先决条件,学习目的 ...
- Windows phone开发 页面布局之屏幕方向
(博客部分内容参考Windows phone开发文档) Windows phone的屏幕方向是利用Windows phone设备的方向传感器提供的数据实现切换的. Windows Phone支持纵向和 ...
- 背水一战 Windows 10 (2) - UI: 概述, 启动屏幕, 屏幕方向
[源码下载] 背水一战 Windows 10 (2) - UI: 概述, 启动屏幕, 屏幕方向 作者:webabcd 介绍背水一战 Windows 10 之 UI UI 设计概述 启动屏幕(闪屏) 屏 ...
- Android开发之屏幕方向
一.处理屏幕方向变化的两种技术 1.锚定方法 2.调整大小和重新定位,这种方法一般是分别为横向和纵向两种模式各自定义用户界面xml界面文件,当方向变化时读取对应的界面配置文件即可. 二.检测屏幕方向改 ...
- 【NX二次开发】获得屏幕矩阵并设置WCS为屏幕方向
说明:获得屏幕矩阵并设置WCS为屏幕方向(Z朝向自己,X轴朝右,Y轴超上). 方法: 1 extern DllExport void ufusr(char *param, int *retcode, ...
- Kinect for Windows SDK开发入门(15):进阶指引 下
Kinect for Windows SDK开发入门(十五):进阶指引 下 上一篇文章介绍了Kinect for Windows SDK进阶开发需要了解的一些内容,包括影像处理Coding4Fun K ...
- Windows Phone开发(19):三维透视效果
原文:Windows Phone开发(19):三维透视效果 三维效果也可以叫透视效果,所以,我干脆叫三维透视效果.理论知识少讲,直接用例开场吧,因为这个三维效果其实很简单,比上一节中的变换更省事,不信 ...
- Windows Phone开发(4):框架和页
原文:Windows Phone开发(4):框架和页 在开如之前,我想更正一个小问题,之前我在第一篇文章中说,Visual Studio Express for Windows Phone的中文版没有 ...
随机推荐
- 如果用float实现居中
今天发现自己做的一个项目中有个图片切换的下面的按钮不是固定个数,程序那边根据循环实现放几个切换的按钮,但是按钮相对于整体的要居中,刚开始想着用display:inline-block;实现,但是ie6 ...
- c 有意思的数组初始化
c 有意思的数组初始化 #include <stdio.h> int main() { int i = 0; char a[1024]; char a0[10] = {}; char a1 ...
- Swift - 标签条(UITabBar)标签页控制器(UITabBarController)用法
App底部的tab标签页可以方便的把功能模块划分清楚,只需点击相应的标签页就可以展示完全独立的视图页面,同时各标签页间的视图也可以进行数据交换. TabBarItem系统自带图标样式(System ...
- codeforces 325B Stadium and Games
这道题思路很简单,设刚开始队伍数为d=2^p*x,其中x是奇数,则比赛场次n=(2^p-1)*x+(x-1)*x/2,然后从0开始枚举p的值,接着解一元二次方程x^2+(2^(p+1)-3)x-2*n ...
- OCA读书笔记(9) - 管理数据同步
9.Managing Data Concurrency 描述锁机制以及oracle如何管理数据一致性监控和解决锁冲突 管理数据的并发--管理锁数据的不一致:脏读更改丢失幻影读 脏读:数据是指事务T2修 ...
- hdu 1007 最近点对问题(Splay解法)
为什么要写这个题..经典啊,当然,别以为我用分治做的,不过主要思想还是那神奇的六个点共存(一个h*2h的矩形中最多能放下多少个点使得两两距离不超过h) 其实我是在这里看到的 http://commun ...
- Android开发5:布局管理器2(表格布局TableLayout)
版本:Android4.3 API18 学习整理:liuxinming 概念 TableLayout继承了LinearLayout,因此它的本质依然是线性布局管理器. 表格布局采 ...
- Linux 利用hosts.deny 防止暴力破解ssh(转)
一.ssh暴力破解 利用专业的破解程序,配合密码字典.登陆用户名,尝试登陆服务器,来进行破解密码,此方法,虽慢,但却很有效果. 二.暴力破解演示 2.1.基础环境:2台linux主机(centos 7 ...
- 极路由1s,固件需要刷入RipOS系统的加40块
极路由1s,固件需要刷入RipOS系统的加40块,集成wifidog功能,wifi广告路由器的理想选择功能. 经过测试,无线性能稳定,无线可带32个手机客户端. 具体配置: 7620CPU ,主频58 ...
- 日交易41.9亿,B2B的魅力为何不输于B2C、C2C?
在最近两年的电子商务版图中,B2C和C2C可谓大放异彩,相比之下,B2B却显得颇为“低调”,当然,低调并不代表没有影响力,只不过,相比B2C和C2C面向数亿网民而言,B2B只针对企业和商家服务 ...