Windows Phone 实用开发技巧(30):Pivot切换时同时渐变背景图片

简介:

上篇文章讲了如果动态绑定Pivot,其实绑定正确了就可以了,没有什么技术难点。今天介绍如果在切换PivotItem时同时渐变的切换Pivot的背景图片,用来提高用户体验。

当 然很多时候如果你的Pivot有背景图片,那经常是一张图片,不会每个PivotItem都给一张图片。但是有时候或许就用这样的需求,不同的Pivot 有不同的背景图片,那么你如何去做到很好的背景图片的切换呢?因为如果背景图片的反差比较大的时候,给用户的体验不是很好。

那么如何实 现很好的过渡效果呢?我的想法是在切换时渐变其背景图片。在刚开始使用Expression Blend的时候遇到一个问题,我们不能对Pivot的背景图片做动画。但是换个思路去想,我们可以对用户控件的透明度做动画,我们可以使用用户控件作为 当前页面的背景动画。

public partial class BgControl : UserControl {
    public DependencyProperty ImageUrlProperty = DependencyProperty.Register("ImageUrl", typeof(string), typeof(BgControl), 
        new PropertyMetadata(new PropertyChangedCallback((e1,e2) =>
        {
            var control = e1 as BgControl;
            if (control != null && e2.NewValue!=null)
            {
                control.LayoutRoot.Background = new ImageBrush() { ImageSource = new BitmapImage(new Uri(e2.NewValue.ToString(), UriKind.Relative)) };
            }
        })));

    public string ImageUrl
    {
        get         {
            return (string)base.GetValue(ImageUrlProperty);
        }
        set         {
            base.SetValue(ImageUrlProperty, value);
        }
   }

    public BgControl()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(BgControl_Loaded);
    }

    void BgControl_Loaded(object sender, RoutedEventArgs e)
    {
        this.LayoutRoot.Background = new ImageBrush() { ImageSource = new BitmapImage(new Uri(ImageUrl, UriKind.Relative)) };
    }
}

上述代码是该背景控件的后置代码,注册了一个依赖属性ImageUrl,在属性发生变化的时候同时修改当前的背景。

ok,下面看看XAML中如何调用这个控件的

<Grid x:Name="LayoutRoot" Background="Transparent">     <local:BgControl x:Name="bgControl" ImageUrl="Bg/1.jpg" />     <controls:Pivot x:Name="pivot" Title="DYNAMIC BG" SelectionChanged="Pivot_SelectionChanged">         <controls:PivotItem Header="pivot one">          </controls:PivotItem>         <controls:PivotItem Header="pivot two">          </controls:PivotItem>         <controls:PivotItem Header="pivot three">          </controls:PivotItem>     </controls:Pivot> </Grid> 

我们将该控件设置很Pivot平级,并且放置在Pivot的前面,这样BgControl就会在Pivot的下方呈现。下面看看Pivot的SelectionChanged事件:

Random r = new Random();
private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    int bg = r.Next(1, 7);
    if (sb_shuffle != null)
    {
        sb_shuffle.Begin();
        pivot.IsHitTestVisible = false;
        sb_shuffle.Completed += (e1, e2) =>
        {
            bgControl.ImageUrl = string.Format("Bg/{0}.jpg", bg);
            sb_next.Begin();
            sb_next.Completed += (e3, e4) =>
                {
                    pivot.IsHitTestVisible = true;
                };
        };

    }
}

随即生成当前背景图片的文件名,然后播放两个动画,一个是当前背景的渐渐消失,一个是下一个背景的渐渐显示。

<phone:PhoneApplicationPage.Resources>     <Storyboard x:Name="sb_shuffle">         <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="bgControl">             <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0"/>         </DoubleAnimationUsingKeyFrames>     </Storyboard>     <Storyboard x:Name="sb_next">         <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="bgControl">             <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>         </DoubleAnimationUsingKeyFrames>     </Storyboard> </phone:PhoneApplicationPage.Resources> 

注意到SelectionChanged事件中,需要将Pivot的IsHitTestVisible设为false,即表示正在播放动画的时候我们不能滑动Pivot,在动画结束的时候再还原回来。

实例代码可以在这里找到。Hope that helps.





     本文转自xshf12345 51CTO博客,原文链接:http://blog.51cto.com/alexis/720462,如需转载请自行联系原作者


相关文章
|
Android开发 iOS开发 Windows
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
不久前,随着最后一家WP手机厂商惠普宣布取消今后Windows Phone的研发计划,以及微软官方声明对WP8.1系统今后所有升级维护的终止,WP手机,作为曾经和安卓手机、苹果手机并驾齐驱的三大智能手机之一,正式寿终正寝。
1248 0
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
|
XML 开发框架 前端开发
Windows Phone快速入门需掌握哪些能力
在此之前,先普及下Windows Phone的概念和开发工具的介绍。 Windows Phone是微软公司开发的手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验集成至手机中。2012年6月21日,微软正式发布Windows Phone 8,采用和Windows 8相同的Windows NT内核,同时也针对市场的Windows Phone 7.5发布Windows Phone 7.8。
133 0
Windows Phone快速入门需掌握哪些能力
|
编解码 前端开发 JavaScript
Windows Phone 下开发 LBS 应用
基于位置的服务(Location Based Service,LBS),它是通过电信移动运营商的无线电通讯网络(如GSM网、CDMA网)或外部定位方式(如GPS)获取移动终端用户的位置信息(地理坐标,或大地坐标),在GIS(Geographic Information System,地理信息系统)平台的支持下,为用户提供相应服务的一种增值业务。
162 0
|
移动开发 Android开发 开发者
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
在Build 2014大会上,微软正式发布了传闻已久的Windows Phone 8.1系统,所有的Windows Phone 8手机都可以升级,微软这次可谓是十分厚道。虽然并非迭代升级,但WP 8.1还是拥有很多重大更新,对于微软进一步完善移动平台拥有积极的意义。下面,就一起来了解一下WP 8.1的主要新特性。
230 0
Windows Phone 8.1 新功能汇总 开发者预览版开放下载