view 代码如下:

View Code
 1     <Grid>
2 <Grid.RowDefinitions>
3 <RowDefinition Height="50"/>
4 <RowDefinition Height="*"/>
5 </Grid.RowDefinitions>
6 <StackPanel Orientation="Horizontal">
7 <Button x:Name="BtnPrevious" Width="100" Height="40" Content="Previous" Click="BtnFlip_Click"/>
8 <Button x:Name="BtnNext" Width="100" Height="40" Content="Next" Margin="20 0 0 0" Click="BtnFlip_Click"/>
9 </StackPanel>
10 <Image x:Name="MyImage" Grid.Row="1" Stretch="Uniform" Width ="200" Height ="200">
11 <Image.Triggers>
12 <EventTrigger RoutedEvent="Image.Loaded">
13 <BeginStoryboard>
14 <Storyboard>
15 <DoubleAnimation Storyboard.TargetName="MyImage"
16 Storyboard.TargetProperty="Width"
17 From="20" To="200" Duration="0:0:2"
18 AutoReverse="True" RepeatBehavior="Forever"/>
19 <DoubleAnimation Storyboard.TargetName="MyImage"
20 Storyboard.TargetProperty="Height"
21 From="20" To="200" Duration="0:0:2"
22 AutoReverse="True" RepeatBehavior="Forever"/>
23 </Storyboard>
24 </BeginStoryboard>
25 </EventTrigger>
26 </Image.Triggers>
27 </Image>
28 </Grid>

code behind 如下:

View Code
 1         private readonly string _strPath;
2 private List<string> _pathList;
3 private int _index;
4
5 public Window2()
6 {
7 InitializeComponent();
8 _strPath = Directory.GetCurrentDirectory();
9 _pathList = new List<string>();
10 this.Loaded += new RoutedEventHandler(Window2_Loaded);
11 }
12
13 void Window2_Loaded(object sender, RoutedEventArgs e)
14 {
15 ShowFirstImg();
16 }
17
18 private void ShowFirstImg()
19 {
20 string[] str = Directory.GetDirectories("Imgs");
21 string[] imgStr = Directory.GetFiles(str[0]);
22 for (int nI = 0; nI < imgStr.Length; nI++)
23 {
24 string fileName = imgStr[nI].ToLower();
25 if (fileName.EndsWith(".bmp", StringComparison.CurrentCulture))
26 {
27 _pathList.Add(imgStr[nI]);
28 }
29 else if (fileName.EndsWith(".gif", StringComparison.CurrentCulture))
30 {
31 _pathList.Add(imgStr[nI]);
32 }
33 else if (fileName.EndsWith(".png", StringComparison.CurrentCulture))
34 {
35 _pathList.Add(imgStr[nI]);
36 }
37 else if (fileName.EndsWith(".jpg", StringComparison.CurrentCulture))
38 {
39 _pathList.Add(imgStr[nI]);
40 }
41 else
42 {
43 continue;
44 }
45 }
46
47 _index = 0;
48 string firstImgPath = _strPath + "\\" + _pathList[_index];
49 MyImage.Source = new BitmapImage(new Uri(firstImgPath));
50 }
51
52 private void BtnFlip_Click(object sender, RoutedEventArgs e)
53 {
54 if (Object.ReferenceEquals(sender, BtnPrevious))
55 {
56 if (_index == 0)
57 {
58 _index = _pathList.Count - 1;
59 }
60 else
61 {
62 _index--;
63 }
64 string strAll = _strPath + "\\" + _pathList[_index];
65 MyImage.Source = new BitmapImage(new Uri(strAll));
66 }
67 else if (Object.ReferenceEquals(sender, BtnNext))
68 {
69 if (_index == _pathList.Count - 1)
70 {
71 _index = 0;
72 }
73 else
74 {
75 _index++;
76 }
77 string strAll = _strPath + "\\" + _pathList[_index];
78 MyImage.Source = new BitmapImage(new Uri(strAll));
79 }
80 else
81 {
82 }
83 }




posted on 2011-12-17 15:45  Damon-Cui  阅读(1060)  评论(0编辑  收藏  举报