Silverlight4 RIA应用开发系列课程-3-Handling page orientation changes

ExpandedBlockStart.gif MainPage.xaml
< phone:PhoneApplicationPage 
    x:Class
= " Orientation.MainPage "
    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 "
    mc:Ignorable
= " d "  d:DesignWidth = " 480 "  d:DesignHeight = " 768 "
    FontFamily
= " {StaticResource PhoneFontFamilyNormal} "
    FontSize
= " {StaticResource PhoneFontSizeNormal} "
    Foreground
= " {StaticResource PhoneForegroundBrush} "
    SupportedOrientations
= " Portrait "  Orientation = " Portrait "
    shell:SystemTray.IsVisible
= " True " >

    
<!-- LayoutRoot  is  the root grid  where  all page content  is  placed -->
    
< Grid x:Name = " LayoutRoot "  Background = " Transparent " >
        
< Grid.RowDefinitions >
            
< RowDefinition Height = " Auto " />
            
< RowDefinition Height = " * " />
        
</ Grid.RowDefinitions >

        
<!-- TitlePanel contains the name of the application and page title -->
        
< StackPanel x:Name = " TitlePanel "  Grid.Row = " 0 "  Margin = " 12,17,0,28 " >
            
< TextBlock x:Name = " ApplicationTitle "  Text = " ORIENTATION DEMO "  Style = " {StaticResource PhoneTextNormalStyle} " />
            
< TextBlock x:Name = " PageTitle "  Text = " Portrait Page "  Margin = " 9,-7,0,0 "  Style = " {StaticResource PhoneTextTitle1Style} " />
        
</ StackPanel >

        
<!-- ContentPanel  -  place additional content here -->
        
< Grid x:Name = " ContentPanel "  Grid.Row = " 1 "  Margin = " 12,0,12,0 " >
            
< Button Content = " Go To Portrait Page "  Height = " 100 "  HorizontalAlignment = " Left "  Margin = " 57,41,0,0 "  Name = " button1 "  VerticalAlignment = " Top "  Width = " 373 "  Click = " onPortraitPage "   />
            
< Button Content = " Go to Landscape Page "  Height = " 100 "  HorizontalAlignment = " Left "  Margin = " 57,147,0,0 "  Name = " button2 "  VerticalAlignment = " Top "  Width = " 373 "  Click = " onLandscapePage "   />
            
< Button Content = " Go to Neutral Page "  Height = " 100 "  HorizontalAlignment = " Left "  Margin = " 57,253,0,0 "  Name = " button3 "  VerticalAlignment = " Top "  Width = " 373 "  Click = " onNeutralPage "   />
        
</ Grid >
    
</ Grid >
</ phone:PhoneApplicationPage >


ExpandedBlockStart.gif MainPage.xaml.cs
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  Orientation
{
    
public   partial   class  MainPage : PhoneApplicationPage
    {
        
//  Constructor
         public  MainPage()
        {
            InitializeComponent();
            SupportedOrientations 
=  SupportedPageOrientation.Portrait  |  SupportedPageOrientation.Landscape;
        }

        
private   void  onPortraitPage( object  sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(
new  Uri( " /PortraitPage.xaml " , UriKind.Relative));
        }

        
private   void  onLandscapePage( object  sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(
new  Uri( " /LandscapePage.xaml " , UriKind.Relative));
        }

        
private   void  onNeutralPage( object  sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(
new  Uri( " /NeutralPage.xaml " , UriKind.Relative));
        }
    }
}

 

ExpandedBlockStart.gif LandscapePage.xaml
< phone:PhoneApplicationPage 
    x:Class
= " Orientation.LandscapePage "
    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
= " Landscape "  Orientation = " Landscape "
    mc:Ignorable
= " d "  d:DesignHeight = " 480 "  d:DesignWidth = " 728 "
    shell:SystemTray.IsVisible
= " True " >

    
<!-- LayoutRoot  is  the root grid  where  all page content  is  placed -->
    
< Grid x:Name = " LayoutRoot "  Background = " Transparent " >
        
< Grid.RowDefinitions >
            
< RowDefinition Height = " Auto " />
            
< RowDefinition Height = " * " />
        
</ Grid.RowDefinitions >

        
<!-- TitlePanel contains the name of the application and page title -->
        
< StackPanel x:Name = " TitlePanel "  Grid.Row = " 0 "  Margin = " 12,17,0,28 " >
            
< TextBlock x:Name = " ApplicationTitle "  Text = " ORIENTATION DEMO "  Style = " {StaticResource PhoneTextNormalStyle} " />
            
< TextBlock x:Name = " PageTitle "  Text = " Landscape Page "  Margin = " 9,-7,0,0 "  Style = " {StaticResource PhoneTextTitle1Style} " />
        
</ StackPanel >

        
<!-- ContentPanel  -  place additional content here -->
        
< Grid x:Name = " ContentPanel "  Grid.Row = " 1 "  Margin = " 12,0,12,0 " ></ Grid >
    
</ Grid >
 
    
<!-- Sample code showing usage of ApplicationBar -->
    
<!--< phone:PhoneApplicationPage.ApplicationBar >
        
< shell:ApplicationBar IsVisible = " True "  IsMenuEnabled = " True " >
            
< shell:ApplicationBarIconButton IconUri = " /Images/appbar_button1.png "  Text = " Button 1 " />
            
< shell:ApplicationBarIconButton IconUri = " /Images/appbar_button2.png "  Text = " Button 2 " />
            
< shell:ApplicationBar.MenuItems >
                
< shell:ApplicationBarMenuItem Text = " MenuItem 1 " />
                
< shell:ApplicationBarMenuItem Text = " MenuItem 2 " />
            
</ shell:ApplicationBar.MenuItems >
        
</ shell:ApplicationBar >
    
</ phone:PhoneApplicationPage.ApplicationBar >-->

</ phone:PhoneApplicationPage >


ExpandedBlockStart.gif PortraitPage.xaml
< phone:PhoneApplicationPage 
    x:Class
= " Orientation.PortraitPage "
    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
= " Portrait "  Orientation = " Portrait "
    mc:Ignorable
= " d "  d:DesignHeight = " 768 "  d:DesignWidth = " 480 "
    shell:SystemTray.IsVisible
= " True " >

    
<!-- LayoutRoot  is  the root grid  where  all page content  is  placed -->
    
< Grid x:Name = " LayoutRoot "  Background = " Transparent " >
        
< Grid.RowDefinitions >
            
< RowDefinition Height = " Auto " />
            
< RowDefinition Height = " * " />
        
</ Grid.RowDefinitions >

        
<!-- TitlePanel contains the name of the application and page title -->
        
< StackPanel x:Name = " TitlePanel "  Grid.Row = " 0 "  Margin = " 12,17,0,28 " >
            
< TextBlock x:Name = " ApplicationTitle "  Text = " ORIENTATION DEMO "  Style = " {StaticResource PhoneTextNormalStyle} " />
            
< TextBlock x:Name = " PageTitle "  Text = " Portrait Page "  Margin = " 9,-7,0,0 "  Style = " {StaticResource PhoneTextTitle1Style} " />
        
</ StackPanel >

        
<!-- ContentPanel  -  place additional content here -->
        
< Grid x:Name = " ContentPanel "  Grid.Row = " 1 "  Margin = " 12,0,12,0 " ></ Grid >
    
</ Grid >
 
    
<!-- Sample code showing usage of ApplicationBar -->
    
<!--< phone:PhoneApplicationPage.ApplicationBar >
        
< shell:ApplicationBar IsVisible = " True "  IsMenuEnabled = " True " >
            
< shell:ApplicationBarIconButton IconUri = " /Images/appbar_button1.png "  Text = " Button 1 " />
            
< shell:ApplicationBarIconButton IconUri = " /Images/appbar_button2.png "  Text = " Button 2 " />
            
< shell:ApplicationBar.MenuItems >
                
< shell:ApplicationBarMenuItem Text = " MenuItem 1 " />
                
< shell:ApplicationBarMenuItem Text = " MenuItem 2 " />
            
</ shell:ApplicationBar.MenuItems >
        
</ shell:ApplicationBar >
    
</ phone:PhoneApplicationPage.ApplicationBar >-->

</ phone:PhoneApplicationPage >


ExpandedBlockStart.gif NeutralPage.xaml
< phone:PhoneApplicationPage 
    x:Class
= " Orientation.NeutralPage "
    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
= " Portrait "  Orientation = " Portrait "
    mc:Ignorable
= " d "  d:DesignHeight = " 768 "  d:DesignWidth = " 480 "
    shell:SystemTray.IsVisible
= " True " >

    
<!-- LayoutRoot  is  the root grid  where  all page content  is  placed -->
    
< Grid x:Name = " LayoutRoot "  Background = " Transparent " >
        
< Grid.RowDefinitions >
            
< RowDefinition Height = " Auto " />
            
< RowDefinition Height = " * " />
        
</ Grid.RowDefinitions >

        
<!-- TitlePanel contains the name of the application and page title -->
        
< StackPanel x:Name = " TitlePanel "  Grid.Row = " 0 "  Margin = " 12,17,0,28 " >
            
< TextBlock x:Name = " ApplicationTitle "  Text = " ORIENTATION DEMO "  Style = " {StaticResource PhoneTextNormalStyle} " />
            
< TextBlock x:Name = " PageTitle "  Text = " Neutral Page "  Margin = " 9,-7,0,0 "  Style = " {StaticResource PhoneTextTitle1Style} " />
        
</ StackPanel >

        
<!-- ContentPanel  -  place additional content here -->
        
< Grid x:Name = " ContentPanel "  Grid.Row = " 1 "  Margin = " 12,0,12,0 " >
            
< TextBlock Height = " 126 "  Margin = " 34,32,32,0 "  Name = " textBlock1 "  Text = " This is a textblock. It should reposition itself to layout in both Landscape and Portrait.  "  VerticalAlignment = " Top "  FontSize = " 24 "  TextWrapping = " Wrap "   />
        
</ Grid >
    
</ Grid >
 
    
<!-- Sample code showing usage of ApplicationBar -->
    
<!--< phone:PhoneApplicationPage.ApplicationBar >
        
< shell:ApplicationBar IsVisible = " True "  IsMenuEnabled = " True " >
            
< shell:ApplicationBarIconButton IconUri = " /Images/appbar_button1.png "  Text = " Button 1 " />
            
< shell:ApplicationBarIconButton IconUri = " /Images/appbar_button2.png "  Text = " Button 2 " />
            
< shell:ApplicationBar.MenuItems >
                
< shell:ApplicationBarMenuItem Text = " MenuItem 1 " />
                
< shell:ApplicationBarMenuItem Text = " MenuItem 2 " />
            
</ shell:ApplicationBar.MenuItems >
        
</ shell:ApplicationBar >
    
</ phone:PhoneApplicationPage.ApplicationBar >-->

</ phone:PhoneApplicationPage >


示例代码

转载于:https://www.cnblogs.com/dnso/articles/1910189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值