WEB开发网
开发学院图形图像Flash SilverLight幻灯片 阅读

SilverLight幻灯片

 2009-03-31 11:58:52 来源:WEB开发网   
核心提示: AD 后台代码 1public partial class AD : UserControl 2 { 3 /** <summary> 4 /// 幻灯片计时器 5 /// </summary> 6 DispatcherTimer timer; 7 /** <s

AD 后台代码

  1public partial class AD : UserControl
  2    {
  3        /**//// <summary>
  4        /// 幻灯片计时器
  5        /// </summary>
  6        DispatcherTimer timer;
  7        /**//// <summary>
  8        /// 记录当前显示的AD
  9        /// </summary>
 10        public ADEntity currentAD;
 11        /**//// <summary>
 12        /// 记录当前正在使用的Image
 13        /// </summary>
 14        Image currentImage;
 15
 16        /**//// <summary>
 17        /// 当前正在显示的AD
 18        /// </summary>
 19        public ADEntity CurrentAD
 20        {
 21            get
 22            {
 23                return currentAD;
 24            }
 25            set
 26            {
 27                //外界可通过更改值,并显示替换图片的动态效果
 28                if(!currentAD.Equals(value))
 29                {
 30                    currentAD = value;
 31                    BeginAnim(value);
 32                }
 33            }
 34        }
 35
 36        /**//// <summary>
 37        /// AD列表
 38        /// </summary>
 39        List<ADEntity> ads = new List<ADEntity>();
 40
 41        /**//// <summary>
 42        /// 构造方法
 43        /// </summary>
 44        public AD ( List<ADEntity> ADs )
 45            : this ()
 46        {
 47            ads = ADs;
 48        }
 49
 50        public AD()
 51        {
 52            InitializeComponent();
 53            //初始化计时器
 54            timer=new DispatcherTimer();
 55            //计时器间隔5秒
 56            timer.Interval = new TimeSpan(0,0,0,5);
 57            timer.Tick += timer_Tick;
 58            timer.Start ();
 59        }
 60
 61        void timer_Tick ( object sender, EventArgs e )
 62        {
 63            for(int i=0;i<ads.Count;i++)
 64            {
 65                if(currentAD.Equals(ads[i]))
 66                {
 67                    //在计时器中循环替换当前AD
 68                    CurrentAD = i==ads.Count-1 ? ads[0] : ads[i + 1];
 69                    break;
 70                }
 71            }
 72        }
 73
 74        private void UserControl_Loaded ( object sender, RoutedEventArgs e )
 75        {
 76            for(int i=0;i<ads.Count;i++)
 77            {
 78                //加载按钮
 79                TiggerButton tb = new TiggerButton(i + 1, ads[i]);
 80                //按钮样式,边距为5
 81                tb.Margin=new Thickness(5);
 82                tb.onSelected += tb_onSelected;
 83                ButtonContainer.Children.Add(tb);
 84            }
 85            if ( ads.Count > 0 )
 86            {
 87                //为当前AD赋初始值
 88                currentAD = ads[0];
 89                Image1.Source = new BitmapImage(new Uri(currentAD.Image, UriKind.Relative));
 90                currentImage = Image1;
 91            }
 92        }
 93
 94        void tb_onSelected ( object sender, EventArgs e )
 95        {
 96            //按钮被选中后,替换当前AD
 97            CurrentAD = ((TiggerButton) sender).ADE;
 98            //重设计时器
 99            timer.Start();
100        }
101
102        /**//// <summary>
103        /// 播放动画
104        /// </summary>
105        public void BeginAnim(ADEntity adEntity)
106        {
107            //构造新故事版
108            Storyboard sb_Opacity = new Storyboard();
109            //图片隐藏动画
110            DoubleAnimation ImageHideAnim = new DoubleAnimation
111                                            {
112                                                //透明度目标为0
113                                                To = 0,
114                                                //1秒动画时间
115                                                Duration = new TimeSpan(0, 0, 1)
116                                            };
117            //图片显示动画
118            DoubleAnimation ImageShowAnim = new DoubleAnimation
119                                            {
120                                                //透明度目标为1
121                                                To = 1,
122                                                //透明度初始值为0
123                                                From=0,
124                                                Duration = new TimeSpan(0, 0, 1)
125                                            };
126            //构造图片URI
127            Uri uri = new Uri(adEntity.Image, UriKind.Relative);
128            if(currentImage==Image1)
129            {
130                //为新图片赋值
131                Image2.Source = new BitmapImage(uri);
132                ImageHideAnim.From = Image1.Opacity;
133                //设置动画对象
134                Storyboard.SetTarget(ImageHideAnim, Image1);
135                Storyboard.SetTarget(ImageShowAnim, Image2);
136                //设置当前Image
137                currentImage = Image2;
138            }
139            else if (currentImage==Image2)
140            {
141                Image1.Source = new BitmapImage(uri);
142                ImageHideAnim.From = Image2.Opacity;
143                Storyboard.SetTarget(ImageHideAnim,Image2);
144                Storyboard.SetTarget(ImageShowAnim,Image1);
145                currentImage = Image1;
146            }
147            //设置动画对象属性
148            Storyboard.SetTargetProperty ( ImageHideAnim, new PropertyPath ( "Opacity" ) );
149            Storyboard.SetTargetProperty ( ImageShowAnim, new PropertyPath ( "Opacity" ) );
150            //把动画添加到故事版中
151            sb_Opacity.Children.Add(ImageHideAnim);
152            sb_Opacity.Children.Add(ImageShowAnim);
153            //开始动画
154            sb_Opacity.Begin();
155        }
156
157        private void UserControl_MouseEnter ( object sender, System.Windows.Input.MouseEventArgs e )
158        {
159            Storyboard sb = new Storyboard();
160            DoubleAnimation da = new DoubleAnimation()
161                                 {
162                                     Duration = new TimeSpan(0,0,1),
163                                     To = 1,
164                                     From = ContainerGrid.Opacity
165                                 };
166            Storyboard.SetTarget(da, ContainerGrid);
167            Storyboard.SetTargetProperty(da, new PropertyPath("Opacity"));
168            sb.Children.Add(da);
169            sb.Begin();
170        }
171
172        private void UserControl_MouseLeave ( object sender, System.Windows.Input.MouseEventArgs e )
173        {
174            Storyboard sb = new Storyboard();
175            DoubleAnimation da = new DoubleAnimation
176                                 {
177                                     Duration = new TimeSpan(0,0,1),
178                                     To = 0,
179                                     From = ContainerGrid.Opacity
180                                 };
181            Storyboard.SetTarget(da, ContainerGrid);
182            Storyboard.SetTargetProperty(da, new PropertyPath("Opacity"));
183            sb.Children.Add(da);
184            sb.Begin();
185        }
186    }

在播放动画的方法中,构造了个故事版,并把TimeLine对象添加进去。

在SL的动画中,有很多类型的TimeLine,这些TimeLine可以指定不同数据类型的变化动画。

在SetTargetProperty方法中,设置的必须是依赖属性。并且这个依赖属性必须是在后台声明的名称,类似

依赖属性

public static readonly DependencyProperty NumberProperty = DependencyProperty.Register("Number",
                                                                                               typeof(int),
                                                                                               typeof(TiggerButton),
                                                                                               new PropertyMetadata(1));

中的Number这个字符串。

XML的读取我就不再赘言了,不过要说明的是SL的运行环境中的XML对象模型,和普通的Framework对象模型完全不同,再也不能用XMLDocument之类的对象了。

上一页  1 2 3 4 

Tags:SilverLight 幻灯片

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接