相信大家都看到过网易的新闻图片幻灯片切换效果,好像Flash和Ajax版本的都有了,我来加一个Sliverlight2版本的。®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
实现平台:VS2008 + Silverlight2®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
下面链接可以直接看运行程序®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
Live Demo®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
效果图:®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
实现要点:®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
1.从配置文件中动态添加Image对象,并且初始Image对象的一些属性:®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
// 添加图片控件®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                Image image = new Image();®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                image.Cursor = System.Windows.Input.Cursors.Hand;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                image.Width = IMAGE_WIDTH;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                image.Height = IMAGE_HEIGHT;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                image.SetValue(Canvas.LeftProperty, 0);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                image.SetValue(Canvas.TopProperty, 0);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                image.SetValue(Canvas.ZIndexProperty, i);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                image.Source = new BitmapImage(new Uri(this.picList.ImageUrl, UriKind.Relative));®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                image.MouseLeftButtonUp += new MouseButtonEventHandler(image_MouseLeftButtonUp);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
Canvas.LeftProperty属性是指相对于容器的左边缘的位置,这里图片的消失显示主要就是利用了这个属性,®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
如果是负值,那么图片将显示在容器左边缘以外的位置,如果这个值大于容器的宽度,那么则显示在容器右边缘®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
以外的位置。图片点击时间自然是弹出图片链接的新闻了。®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
        void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
        {®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            HtmlPage.Window.Navigate(new Uri(this.picList[this.picIndex].Href), "_blank");®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
        }®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
图片属性设定完成后开始添加飞行效果的动作了:®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
  // 添加动作®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                Storyboard driftStoryboard = new Storyboard();®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                Duration driftDuration = new Duration(TimeSpan.FromSeconds(1));®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                DoubleAnimation driftAnimation = new DoubleAnimation();®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                driftAnimation.Duration = driftDuration;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                Storyboard.SetTarget(driftAnimation, image);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                // 对左面边框的位置®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                Storyboard.SetTargetProperty(driftAnimation, "(Canvas.Left)");®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                driftStoryboard.Children.Add(driftAnimation);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                driftStoryboard.Duration = driftDuration;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                driftStoryboard.Completed += new EventHandler(driftStoryboard_Completed);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                Backgroud.Children.Add(image);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                // 添加到资源中®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                Backgroud.Resources.Add(driftStoryboard);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                this._animationMap.Add(i, driftStoryboard);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                this._imageMap.Add(i, image);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
new Duration(TimeSpan.FromSeconds(1)是指一秒钟内完成飞行动作,®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
Storyboard.SetTargetProperty(driftAnimation, "(Canvas.Left)");®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
则是说这个动画设置的是图片的Canvas.Left 的属性,这个属性已经在上面说明过了。®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
然后就是设置定时器以不断的促发这个动画了:®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
  // 定时器®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            this.timer = new DispatcherTimer();®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            this.timer.Interval = TimeSpan.FromSeconds(2);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            this.timer.Tick += new EventHandler(timer_Tick);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            this.timer.Start();®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
在定时器方法里:®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
        // 定时切换图片®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
        void timer_Tick(object sender, EventArgs e)®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
        {®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            int nextIndex = this.picIndex + 1;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            if (this.picList.Count == nextIndex)®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                nextIndex = 0;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            // 现在的图片的动作®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            DoubleAnimation currentAnimation = (DoubleAnimation)this._animationMap[this.picIndex].Children[0];®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            // 要显示的图片的动作®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            DoubleAnimation nextAnimation = (DoubleAnimation)this._animationMap[nextIndex].Children[0];®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            // 从右到左消失®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            currentAnimation.From = 0;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            currentAnimation.To = 0 - IMAGE_WIDTH;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            // 从右到左显示®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            nextAnimation.From = IMAGE_WIDTH;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            nextAnimation.To = 0;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            // 调整图片显示顺序®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            for (int i = 0; i < this.picList.Count; i++)®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            {®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                this._imageMap.SetValue(Canvas.ZIndexProperty, i);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                if (i == nextIndex) ®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                    this._imageMap.SetValue(Canvas.ZIndexProperty, this.picList.Count);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                else if(i == this.picIndex)®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                    this._imageMap.SetValue(Canvas.ZIndexProperty, this.picList.Count - 1);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            }®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            ®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            this._animationMap[this.picIndex].Begin();®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            this._animationMap[nextIndex].Begin();®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            // 设置新的Title和选中的按钮®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            Picture pic = this.picList[nextIndex];®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            this.titleText.Text = pic.Title;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            ButtonNo buttonno = this._ButtonMap[this.picIndex];®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            buttonno.txtnum.Foreground = new SolidColorBrush(Colors.White);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            buttonno.rect.Fill = new SolidColorBrush(Colors.Black);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            buttonno = this._ButtonMap[nextIndex];®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            buttonno.txtnum.Foreground = new SolidColorBrush(Colors.Black);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            buttonno.rect.Fill = new SolidColorBrush(Colors.Red);®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            // 保存当前的图片索引®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            this.picIndex = nextIndex;®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
        }®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
这一段主要是指定动画的开始点和结束点,然后调整下面的数字按钮。®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
数字按钮的事件代码和定时器的类似,大家可以直接看源代码。®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
重点的部分都说完了,最后测试画面的时候要修改一下TestPage.html页面,®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
原来显示Sliverlight控件部分要修改成®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
    <div id="silverlightControlHost">®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
        <object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" Width="282" Height="408">®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            <param name="source" value="PictureSlider.xap"/>®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            <param name="onerror" value="onSilverlightError" />®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            <param name="background" value="white" />®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            ®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            <a href="http://go.microsoft.com/fwlink/?LinkID=108182" style="text-decoration: none;">®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
            </a>®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
        </object>®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
        <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
    </div>®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
原来是Width="100%" Height="100%" 改成容器的大小  Width="282" Height="408"®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
否则图片会显示在容器之外了。®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
这样一个初步的图片轮转效果就完成了,还有很多需要修改的地方,我会继续完善它的,开发它的后续版本。®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
如果有兴趣的朋友可以一起讨论探究哦。®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤
代码下载:http://www.cnblogs.com/Files/ithurricane/PictureSlider.zip
®B¸G“„@˜www.netcsharp.cnù3hî‰6,I3¤