silverlight版的图片轮换广告
今天下午模仿公司的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>
Xaml.cs代码:

Xaml.Cs
1
using System;
2
using System.Collections.Generic;
3
using System.Json;
4
using System.Windows.Controls;
5
using System.Windows.Input;
6
using System.Windows.Media.Effects;
7
using System.Windows.Media.Imaging;
8
using System.Windows.Threading;
9
10
11
namespace 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 <= 0)
100
{
101
currentIndex = listSrc.Count;
102
}
103
LoadImage();
104
}
105
106
/**//// <summary>
107
/// 点击向下按钮时的逻辑处理
108
/// </summary>
109
/// <param name="sender"></param>
110
/// <param name="e"></param>
111
private void down(object sender, MouseButtonEventArgs e)
112
{
113
currentIndex++;
114
if (currentIndex >= listSrc.Count)
115
{
116
currentIndex = 0;
117
}
118
LoadImage();
119
}
120
121
/**//// <summary>
122
/// 单击右侧小图时,同时步更换大图
123
/// </summary>
124
/// <param name="sender"></param>
125
/// <param name="e"></param>
126
private void ImgClick(object sender, MouseButtonEventArgs e)
127
{
128
Image imgSmall = sender as Image;
129
130
imgBig.Source = imgSmall.Source;
131
sb_Big.Begin();
132
133
for (int i = 1; i <= 4; i++)
134
{
135
Image img = this.pnl2.FindName("img" + i.ToString()) as Image;
136
if (img == imgSmall)
137
{
138
img.Effect = new DropShadowEffect();
139
currentImgIndex = i-1;//重新保存新的小图阴影索引
140
}
141
else
142
{
143
img.Effect = null;
144
}
145
}
146
147
//确定新的currentIndex
148
for (int i = 0; i < listSrc.Count; i++)
149
{
150
if ((imgSmall.Source as BitmapImage).UriSource == new Uri(listSrc[i].src, UriKind.Relative))
151
{
152
currentIndex = i;
153
break;
154
}
155
}
156
txtImg.Text = listSrc[currentIndex].name + " - yjmyzz.cnblogs.com"; ;
157
}
158
159
/**//// <summary>
160
/// 自定义类
161
/// </summary>
162
public class ImageItem
163
{
164
public string src
{ set; get; }
165
public string name
{ set; get; }
166
}
167
168
private void imgDown_MouseEnter(object sender, MouseEventArgs e)
169
{
170
this._timer.Stop();
171
}
172
173
private void imgDown_MouseLeave(object sender, MouseEventArgs e)
174
{
175
this._timer.Start();
176
}
177
178
179
}
180
}
源代码下载:https://files.cnblogs.com/yjmyzz/ImageAd_src.rar
xaml代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

Xaml.cs代码:


1

2

3

4

5

6

7

8

9

10

11

12



13

14



15


16

17

18

19

20

21

22

23

24

25

26

27

28

29



30

31

32

33


34

35

36



37



38

39

40

41

42

43

44

45


46

47

48

49

50

51

52

53

54



55

56

57

58

59


60

61

62

63



64

65

66

67



68

69



70

71

72

73

74

75

76



77

78

79

80

81

82

83



84

85

86

87

88

89

90

91


92

93

94

95

96

97



98

99

100



101

102

103

104

105

106


107

108

109

110

111

112



113

114

115



116

117

118

119

120

121


122

123

124

125

126

127



128

129

130

131

132

133

134



135

136

137



138

139

140

141

142



143

144

145

146

147

148

149



150

151



152

153

154

155

156

157

158

159


160

161

162

163



164



165



166

167

168

169



170

171

172

173

174



175

176

177

178

179

180

源代码下载:https://files.cnblogs.com/yjmyzz/ImageAd_src.rar
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
标签:
silverlight
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!