WP8 Orientation change animations

做~自己de王妃 提交于 2020-01-12 09:07:26

问题


What is the easiest way to add orientation change animations to my Windows Phone 8 application? I am interested in something that looks like in native apps like Messages, Calendar, etc. I was looking for a quick and simple solution and the only thing I found working was DynamicOrientionChanges library in NuGet, but it has a huge problem with framerate on Windows Phone 8.


回答1:


You can use the Windows.Phone.Toolkit and handle the OrientationChangedEvent, as showcased here:

http://mobileworld.appamundi.com/blogs/andywigley/archive/2010/11/23/windows-phone-7-page-orientation-change-animations.aspx

I'll copy the source code part of the linked article here, just in case the page goes offline. It includes additional logic to track from which orientation the current one came, so that the animation matches the change:

public partial class MainPage : PhoneApplicationPage
{
    PageOrientation lastOrientation;

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);

        lastOrientation = this.Orientation;
    }

    void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
    {
        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;
    }
}


来源:https://stackoverflow.com/questions/19476025/wp8-orientation-change-animations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!