none
请问一下手机横竖屏切换的时候,怎么设置旋转动画? RRS feed

答案

  • 你好,

    对于处理手机横竖屏切换,你可以使用OrientationChanged事件。

    首先设置手机支持横竖屏
    SupportedOrientations="PortraitOrLandscape"
    然后在后台代码中添加OrientationChanged处理事件

    public MainPage()
        {
            InitializeComponent();
    
            this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);
    
            lastOrientation = this.Orientation;
        }
    

     然后在MainPage_OrientationChanged中编写动画代码,例如:

    void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
            {
                //throw new NotImplementedException();
                PageOrientation newOrientation = e.Orientation;
                //Debug.WriteLine("New orientation: " + newOrientation.ToString());
    
                // Orientations are (clockwise) 'PortraitUp', 'LandscapeRight', 'LandscapeLeft'
    
                RotateTransition transitionElement = new RotateTransition();
    
                switch (newOrientation)
                {
                    case PageOrientation.Landscape:
                    case PageOrientation.LandscapeRight:
                        // Come here from PortraitUp (i.e. clockwise) or LandscapeLeft?
                        if (lastOrientation == PageOrientation.PortraitUp)
                            transitionElement.Mode = RotateTransitionMode.In90Counterclockwise;
                        else
                            transitionElement.Mode = RotateTransitionMode.In180Clockwise;
                        break;
                    case PageOrientation.LandscapeLeft:
                        // Come here from LandscapeRight or PortraitUp?
                        if (lastOrientation == PageOrientation.LandscapeRight)
                            transitionElement.Mode = RotateTransitionMode.In180Counterclockwise;
                        else
                            transitionElement.Mode = RotateTransitionMode.In90Clockwise;
                        break;
                    case PageOrientation.Portrait:
                    case PageOrientation.PortraitUp:
                        // Come here from LandscapeLeft or LandscapeRight?
                        if (lastOrientation == PageOrientation.LandscapeLeft)
                            transitionElement.Mode = RotateTransitionMode.In90Counterclockwise;
                        else
                            transitionElement.Mode = RotateTransitionMode.In90Clockwise;
                        break;
                    default:
                        break;
                }
    
                // Execute the transition
                PhoneApplicationPage phoneApplicationPage = (PhoneApplicationPage)(((PhoneApplicationFrame)Application.Current.RootVisual)).Content;
                ITransition transition = transitionElement.GetTransition(phoneApplicationPage);
                transition.Completed += delegate
                {
                    transition.Stop();
                };
                transition.Begin();
    
                lastOrientation = newOrientation;
    
            }
    


    该代码里使用了windows phone toolkit里的Microsoft.Phone.Controls.Toolkit dll, 效果如下:

     

     


    Mark Yu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 海星C 2012年12月13日 8:21
    2012年12月12日 9:37
    版主

全部回复

  • 你好,

    对于处理手机横竖屏切换,你可以使用OrientationChanged事件。

    首先设置手机支持横竖屏
    SupportedOrientations="PortraitOrLandscape"
    然后在后台代码中添加OrientationChanged处理事件

    public MainPage()
        {
            InitializeComponent();
    
            this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);
    
            lastOrientation = this.Orientation;
        }
    

     然后在MainPage_OrientationChanged中编写动画代码,例如:

    void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
            {
                //throw new NotImplementedException();
                PageOrientation newOrientation = e.Orientation;
                //Debug.WriteLine("New orientation: " + newOrientation.ToString());
    
                // Orientations are (clockwise) 'PortraitUp', 'LandscapeRight', 'LandscapeLeft'
    
                RotateTransition transitionElement = new RotateTransition();
    
                switch (newOrientation)
                {
                    case PageOrientation.Landscape:
                    case PageOrientation.LandscapeRight:
                        // Come here from PortraitUp (i.e. clockwise) or LandscapeLeft?
                        if (lastOrientation == PageOrientation.PortraitUp)
                            transitionElement.Mode = RotateTransitionMode.In90Counterclockwise;
                        else
                            transitionElement.Mode = RotateTransitionMode.In180Clockwise;
                        break;
                    case PageOrientation.LandscapeLeft:
                        // Come here from LandscapeRight or PortraitUp?
                        if (lastOrientation == PageOrientation.LandscapeRight)
                            transitionElement.Mode = RotateTransitionMode.In180Counterclockwise;
                        else
                            transitionElement.Mode = RotateTransitionMode.In90Clockwise;
                        break;
                    case PageOrientation.Portrait:
                    case PageOrientation.PortraitUp:
                        // Come here from LandscapeLeft or LandscapeRight?
                        if (lastOrientation == PageOrientation.LandscapeLeft)
                            transitionElement.Mode = RotateTransitionMode.In90Counterclockwise;
                        else
                            transitionElement.Mode = RotateTransitionMode.In90Clockwise;
                        break;
                    default:
                        break;
                }
    
                // Execute the transition
                PhoneApplicationPage phoneApplicationPage = (PhoneApplicationPage)(((PhoneApplicationFrame)Application.Current.RootVisual)).Content;
                ITransition transition = transitionElement.GetTransition(phoneApplicationPage);
                transition.Completed += delegate
                {
                    transition.Stop();
                };
                transition.Begin();
    
                lastOrientation = newOrientation;
    
            }
    


    该代码里使用了windows phone toolkit里的Microsoft.Phone.Controls.Toolkit dll, 效果如下:

     

     


    Mark Yu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 海星C 2012年12月13日 8:21
    2012年12月12日 9:37
    版主
  • 你好,

    对于处理手机横竖屏切换,你可以使用OrientationChanged事件。

    首先设置手机支持横竖屏
    SupportedOrientations="PortraitOrLandscape"
    然后在后台代码中添加OrientationChanged处理事件

    public MainPage()
        {
            InitializeComponent();
    
            this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);
    
            lastOrientation = this.Orientation;
        }
    

     然后在MainPage_OrientationChanged中编写动画代码,例如:

    void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
            {
                //throw new NotImplementedException();
                PageOrientation newOrientation = e.Orientation;
                //Debug.WriteLine("New orientation: " + newOrientation.ToString());
    
                // Orientations are (clockwise) 'PortraitUp', 'LandscapeRight', 'LandscapeLeft'
    
                RotateTransition transitionElement = new RotateTransition();
    
                switch (newOrientation)
                {
                    case PageOrientation.Landscape:
                    case PageOrientation.LandscapeRight:
                        // Come here from PortraitUp (i.e. clockwise) or LandscapeLeft?
                        if (lastOrientation == PageOrientation.PortraitUp)
                            transitionElement.Mode = RotateTransitionMode.In90Counterclockwise;
                        else
                            transitionElement.Mode = RotateTransitionMode.In180Clockwise;
                        break;
                    case PageOrientation.LandscapeLeft:
                        // Come here from LandscapeRight or PortraitUp?
                        if (lastOrientation == PageOrientation.LandscapeRight)
                            transitionElement.Mode = RotateTransitionMode.In180Counterclockwise;
                        else
                            transitionElement.Mode = RotateTransitionMode.In90Clockwise;
                        break;
                    case PageOrientation.Portrait:
                    case PageOrientation.PortraitUp:
                        // Come here from LandscapeLeft or LandscapeRight?
                        if (lastOrientation == PageOrientation.LandscapeLeft)
                            transitionElement.Mode = RotateTransitionMode.In90Counterclockwise;
                        else
                            transitionElement.Mode = RotateTransitionMode.In90Clockwise;
                        break;
                    default:
                        break;
                }
    
                // Execute the transition
                PhoneApplicationPage phoneApplicationPage = (PhoneApplicationPage)(((PhoneApplicationFrame)Application.Current.RootVisual)).Content;
                ITransition transition = transitionElement.GetTransition(phoneApplicationPage);
                transition.Completed += delegate
                {
                    transition.Stop();
                };
                transition.Begin();
    
                lastOrientation = newOrientation;
    
            }
    


    该代码里使用了windows phone toolkit里的Microsoft.Phone.Controls.Toolkit dll, 效果如下:

     

     


    Mark Yu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    谢谢了MarkYu 帮我学会了很多 呵呵~

    2012年12月13日 8:23