今天下午模仿公司的Flash版图片广告做了一个silverlight版的图片轮换广告,10秒轮换一次
xaml代码:
xaml
1<UserControl
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 x:Class="Ad.Page"
5 >
6 <UserControl.Resources>
7 <Storyboard x:Name="sb_Big">
8 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="imgBig" Storyboard.TargetProperty="(UIElement.Opacity)">
9 <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0.1"/>
10 <EasingDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
11 </DoubleAnimationUsingKeyFrames>
12 </Storyboard>
13 </UserControl.Resources>
14
15 <Canvas Background="#efefef" Height="190" Width="270">
16 <StackPanel Height="170" x:Name="pnl1" Width="270" Orientation="Horizontal" >
17 <Image Height="150" Name="imgBig" Stretch="Fill" Width="200" Margin="10" Source="/Ad;component/img/001.jpg" Cursor="Hand" >
18 <Image.Effect>
19 <DropShadowEffect/>
20 </Image.Effect>
21 </Image>
22 <StackPanel Height="170" x:Name="pnl2" Width="40" Orientation="Vertical" Margin="0,0,10,0">
23 <Image Height="10" Name="imgUp" Stretch="Fill" Width="22" Source="/Ad;component/img/up.png" MouseLeftButtonDown="up" Cursor="Hand"/>
24 <Image Height="30" Name="img1" Stretch="Fill" Width="40" Source="/Ad;component/img/001.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand" />
25 <Image Height="30" Name="img2" Stretch="Fill" Width="40" Margin="0,10,0,0" Source="/Ad;component/img/002.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand">
26
27 </Image>
28 <Image Height="30" Name="img3" Stretch="Fill" Width="40" Margin="0,10,0,0" Source="/Ad;component/img/003.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand"/>
29 <Image Height="30" Name="img4" Stretch="Fill" Width="40" Margin="0,10,0,0" Source="/Ad;component/img/004.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand"/>
30 <Image Height="10" Name="imgDown" Stretch="Fill" Width="22" Source="/Ad;component/img/down.png" MouseLeftButtonDown="down" Cursor="Hand"/>
31 </StackPanel>
32 </StackPanel>
33 <TextBlock Name="txtImg" Canvas.Top="170" Canvas.Left="10" Width="200" Text="就绪" HorizontalAlignment="Center" IsHitTestVisible="False" TextAlignment="Center"></TextBlock>
34 </Canvas>
35</UserControl>
1<UserControl
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 x:Class="Ad.Page"
5 >
6 <UserControl.Resources>
7 <Storyboard x:Name="sb_Big">
8 <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="imgBig" Storyboard.TargetProperty="(UIElement.Opacity)">
9 <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0.1"/>
10 <EasingDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
11 </DoubleAnimationUsingKeyFrames>
12 </Storyboard>
13 </UserControl.Resources>
14
15 <Canvas Background="#efefef" Height="190" Width="270">
16 <StackPanel Height="170" x:Name="pnl1" Width="270" Orientation="Horizontal" >
17 <Image Height="150" Name="imgBig" Stretch="Fill" Width="200" Margin="10" Source="/Ad;component/img/001.jpg" Cursor="Hand" >
18 <Image.Effect>
19 <DropShadowEffect/>
20 </Image.Effect>
21 </Image>
22 <StackPanel Height="170" x:Name="pnl2" Width="40" Orientation="Vertical" Margin="0,0,10,0">
23 <Image Height="10" Name="imgUp" Stretch="Fill" Width="22" Source="/Ad;component/img/up.png" MouseLeftButtonDown="up" Cursor="Hand"/>
24 <Image Height="30" Name="img1" Stretch="Fill" Width="40" Source="/Ad;component/img/001.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand" />
25 <Image Height="30" Name="img2" Stretch="Fill" Width="40" Margin="0,10,0,0" Source="/Ad;component/img/002.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand">
26
27 </Image>
28 <Image Height="30" Name="img3" Stretch="Fill" Width="40" Margin="0,10,0,0" Source="/Ad;component/img/003.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand"/>
29 <Image Height="30" Name="img4" Stretch="Fill" Width="40" Margin="0,10,0,0" Source="/Ad;component/img/004.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand"/>
30 <Image Height="10" Name="imgDown" Stretch="Fill" Width="22" Source="/Ad;component/img/down.png" MouseLeftButtonDown="down" Cursor="Hand"/>
31 </StackPanel>
32 </StackPanel>
33 <TextBlock Name="txtImg" Canvas.Top="170" Canvas.Left="10" Width="200" Text="就绪" HorizontalAlignment="Center" IsHitTestVisible="False" TextAlignment="Center"></TextBlock>
34 </Canvas>
35</UserControl>
Xaml.cs代码:
Xaml.Cs
1using System;
2using System.Collections.Generic;
3using System.Json;
4using System.Windows.Controls;
5using System.Windows.Input;
6using System.Windows.Media.Effects;
7using System.Windows.Media.Imaging;
8using System.Windows.Threading;
9
10
11namespace Ad
12{
13 public partial class Page : UserControl
14 {
15 /**//// <summary>
16 /// Json数据源
17 /// </summary>
18 string imgData = "[{src:'/Ad;component/img/001.jpg',name:'图片1'},{src:'/Ad;component/img/002.jpg',name:'图片2'},{src:'/Ad;component/img/003.jpg',name:'图片3'},{src:'/Ad;component/img/004.jpg',name:'图片4'},{src:'/Ad;component/img/005.jpg',name:'图片5'},{src:'/Ad;component/img/006.jpg',name:'图片6'},{src:'/Ad;component/img/007.jpg',name:'图片7'}]";
19
20 int currentIndex = 0;//当前imgData的索引
21 int currentImgIndex = 0;//当前第几张小图为阴影区
22 int _Max = 4;//右侧显示几张小图
23
24 List<ImageItem> listSrc = new List<ImageItem>();
25
26 private DispatcherTimer _timer;
27
28 public Page()
29 {
30 // 需要初始化变量
31 InitializeComponent();
32
33 将Json转化为强类型的List#region 将Json转化为强类型的List<>
34 JsonValue jv = JsonArray.Parse(imgData);
35 for (int i = 0; i < jv.Count; i++)
36 {
37 listSrc.Add(new ImageItem() { src = jv[i]["src"].ToString().Replace("\\/", "/").Replace("\"", ""), name = jv[i]["name"].ToString().Replace("\\/", "/").Replace("\"", "") });
38 }
39 #endregion
40
41 currentIndex = 0;
42 currentImgIndex = 0;
43 LoadImage();
44
45 启动定时器#region 启动定时器
46 _timer = new DispatcherTimer();
47 _timer.Interval = new TimeSpan(0, 0, 10);
48 _timer.Tick += new EventHandler(_timer_Tick);
49 _timer.Start();
50#endregion
51 }
52
53 void _timer_Tick(object sender, EventArgs e)
54 {
55 down(sender,null);
56 }
57
58
59 /**//// <summary>
60 /// 加载图片
61 /// </summary>
62 private void LoadImage()
63 {
64 int _start = currentIndex % listSrc.Count;
65
66 for (int i = 0; i < _Max; i++)
67 {
68 if (_start >= listSrc.Count)
69 {
70 _start = _start % listSrc.Count;
71 }
72 Image img = this.pnl2.FindName("img" + (i + 1).ToString()) as Image;
73 img.Source = new BitmapImage(new Uri(listSrc[_start].src, UriKind.Relative));
74
75 if (i == currentImgIndex)
76 {
77 img.Effect = new DropShadowEffect();
78 imgBig.Source = img.Source;
79 sb_Big.Begin();
80 txtImg.Text = listSrc[_start].name + " - yjmyzz.cnblogs.com";
81 }
82 else
83 {
84 img.Effect = null;
85 }
86
87 _start++;
88 }
89 }
90
91 /**//// <summary>
92 /// 点击向上翻时的逻辑处理
93 /// </summary>
94 /// <param name="sender"></param>
95 /// <param name="e"></param>
96 private void up(object sender, MouseButtonEventArgs e)
97 {
98 currentIndex--;
99 if (currentIndex <=
1using System;
2using System.Collections.Generic;
3using System.Json;
4using System.Windows.Controls;
5using System.Windows.Input;
6using System.Windows.Media.Effects;
7using System.Windows.Media.Imaging;
8using System.Windows.Threading;
9
10
11namespace Ad
12{
13 public partial class Page : UserControl
14 {
15 /**//// <summary>
16 /// Json数据源
17 /// </summary>
18 string imgData = "[{src:'/Ad;component/img/001.jpg',name:'图片1'},{src:'/Ad;component/img/002.jpg',name:'图片2'},{src:'/Ad;component/img/003.jpg',name:'图片3'},{src:'/Ad;component/img/004.jpg',name:'图片4'},{src:'/Ad;component/img/005.jpg',name:'图片5'},{src:'/Ad;component/img/006.jpg',name:'图片6'},{src:'/Ad;component/img/007.jpg',name:'图片7'}]";
19
20 int currentIndex = 0;//当前imgData的索引
21 int currentImgIndex = 0;//当前第几张小图为阴影区
22 int _Max = 4;//右侧显示几张小图
23
24 List<ImageItem> listSrc = new List<ImageItem>();
25
26 private DispatcherTimer _timer;
27
28 public Page()
29 {
30 // 需要初始化变量
31 InitializeComponent();
32
33 将Json转化为强类型的List#region 将Json转化为强类型的List<>
34 JsonValue jv = JsonArray.Parse(imgData);
35 for (int i = 0; i < jv.Count; i++)
36 {
37 listSrc.Add(new ImageItem() { src = jv[i]["src"].ToString().Replace("\\/", "/").Replace("\"", ""), name = jv[i]["name"].ToString().Replace("\\/", "/").Replace("\"", "") });
38 }
39 #endregion
40
41 currentIndex = 0;
42 currentImgIndex = 0;
43 LoadImage();
44
45 启动定时器#region 启动定时器
46 _timer = new DispatcherTimer();
47 _timer.Interval = new TimeSpan(0, 0, 10);
48 _timer.Tick += new EventHandler(_timer_Tick);
49 _timer.Start();
50#endregion
51 }
52
53 void _timer_Tick(object sender, EventArgs e)
54 {
55 down(sender,null);
56 }
57
58
59 /**//// <summary>
60 /// 加载图片
61 /// </summary>
62 private void LoadImage()
63 {
64 int _start = currentIndex % listSrc.Count;
65
66 for (int i = 0; i < _Max; i++)
67 {
68 if (_start >= listSrc.Count)
69 {
70 _start = _start % listSrc.Count;
71 }
72 Image img = this.pnl2.FindName("img" + (i + 1).ToString()) as Image;
73 img.Source = new BitmapImage(new Uri(listSrc[_start].src, UriKind.Relative));
74
75 if (i == currentImgIndex)
76 {
77 img.Effect = new DropShadowEffect();
78 imgBig.Source = img.Source;
79 sb_Big.Begin();
80 txtImg.Text = listSrc[_start].name + " - yjmyzz.cnblogs.com";
81 }
82 else
83 {
84 img.Effect = null;
85 }
86
87 _start++;
88 }
89 }
90
91 /**//// <summary>
92 /// 点击向上翻时的逻辑处理
93 /// </summary>
94 /// <param name="sender"></param>
95 /// <param name="e"></param>
96 private void up(object sender, MouseButtonEventArgs e)
97 {
98 currentIndex--;
99 if (currentIndex <=