利用silverlight的导航控件,实现翻页效果

具体思路是:

  1. 利用    <PlaneProjection ></PlaneProjection>  对Frame做一个旋转动画。
  2.  在ContentFrame_Navigating 方法里,记录此时的导航页的图片。

      void ContentFrame_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            LastFrameContent = new WriteableBitmap(ContentFrame, null);
        }
并在 ContentFrame_Navigated(object sender, NavigationEventArgs e)的方法里赋值给模板页(此时我们叫模板页)

  private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            FrameMaskImage.Source = LastFrameContent;
            FrameStory.Begin();
        }

3. 对第二步骤中的图片做从显示到隐藏的动画。

4.对Frame控件做从无到有的动画效果,合起来就是翻页效果了。

具体XMAL文件代码:

    <Grid Name="FrameParentPanel" Margin="26,0,26,0" Grid.Row="2">
                <Grid.Projection>
                    <PlaneProjection ></PlaneProjection>
                </Grid.Projection>
                <navigation:Frame x:Name="ContentFrame" Style="{StaticResource ContentFrameStyle}" 
                              Source="/Home" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed">
                <navigation:Frame.UriMapper>
                  <uriMapper:UriMapper>
                    <uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
                    <uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
                  </uriMapper:UriMapper>
                </navigation:Frame.UriMapper>
            </navigation:Frame>

                <!--遮盖层-->
                <Image Name="FrameMaskImage" VerticalAlignment="Top"></Image>
            </Grid>
具提.cs代码:

 public partial class MainPage : UserControl
    {
        private Storyboard FrameStory { get; set; }
        private WriteableBitmap LastFrameContent { get; set; }
        private const int RotateLag = 300;
        private const int RotateTimeStep = 50;
        private const int RotateAngleStep = 15;

        public MainPage()
        {
            InitializeComponent();
            this.ContentFrame.Navigating += new NavigatingCancelEventHandler(ContentFrame_Navigating);
            InitFrameStory();
        }

        void ContentFrame_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            LastFrameContent = new WriteableBitmap(ContentFrame, null);
        }

        void InitFrameStory()
        {
            //旋转动画
            var pageRotateAnimation = new DoubleAnimationUsingKeyFrames();

            for (var i = 0; i <= 12; i++)
            {
                pageRotateAnimation.KeyFrames.Add(new DiscreteDoubleKeyFrame() { Value = RotateAngleStep * (i < 6 ? i : (i - 12)), KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(RotateLag + RotateTimeStep * i)) });
            }

            //遮盖层动画
            var maskAnimation = new ObjectAnimationUsingKeyFrames();
            maskAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame() { Value = Visibility.Visible, KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)) });
            maskAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame() { Value = Visibility.Collapsed, KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(RotateLag + RotateTimeStep * 6)) });

            //frame控件动画
            var frameAnimation = new DoubleAnimationUsingKeyFrames();
            frameAnimation.KeyFrames.Add(new DiscreteDoubleKeyFrame() { Value = 0.0, KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)) });
            frameAnimation.KeyFrames.Add(new DiscreteDoubleKeyFrame() { Value = 1.0, KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(RotateLag + RotateTimeStep * 6)) });

            FrameStory = new Storyboard();
            FrameStory.Children.Add(pageRotateAnimation);
            FrameStory.Children.Add(maskAnimation);
            FrameStory.Children.Add(frameAnimation);

            Storyboard.SetTarget(pageRotateAnimation, FrameParentPanel.Projection);
            Storyboard.SetTarget(maskAnimation, FrameMaskImage);
            Storyboard.SetTarget(frameAnimation, ContentFrame);
            Storyboard.SetTargetProperty(pageRotateAnimation, new PropertyPath(PlaneProjection.RotationYProperty));
            Storyboard.SetTargetProperty(maskAnimation, new PropertyPath(VisibilityProperty));
            Storyboard.SetTargetProperty(frameAnimation, new PropertyPath(OpacityProperty));
        }

        // After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            FrameMaskImage.Source = LastFrameContent;
            FrameStory.Begin();

            foreach (UIElement child in LinksStackPanel.Children)
            {
                HyperlinkButton hb = child as HyperlinkButton;
                if (hb != null && hb.NavigateUri != null)
                {
                    if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
                    {
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
                    }
                }
            }
        }
        // If an error occurs during navigation, show an error window
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }
    }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值