Silverlight4 幻灯片实现

简介: 首先看效果图,然后给出代码。     每10秒自动切换图片,点击右侧图片列表,左侧自动显示该图片。

首先看效果图,然后给出代码。

 

 

每10秒自动切换图片,点击右侧图片列表,左侧自动显示该图片。

 

<UserControl x:Class="gqfc.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" > <UserControl.Resources> <Storyboard x:Name="sb_Big"> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="imgBig" Storyboard.TargetProperty="(UIElement.Opacity)"> <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0.1"/> <EasingDoubleKeyFrame KeyTime="00:00:01" Value="1"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White" Margin="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.85*"></ColumnDefinition> <ColumnDefinition Width="0.15*"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="0.05*"></RowDefinition> <RowDefinition Height="0.95*"></RowDefinition> </Grid.RowDefinitions> <Grid Grid.Column="0" Grid.Row="1"> <Image Name="imgBig" Margin="5,5,1,10" Source="/gqfc;component/Photos/fc1.jpg" Cursor="Hand" Stretch="Fill" > <Image.Effect> <DropShadowEffect/> </Image.Effect> </Image> </Grid> <Grid Grid.Column="1" Grid.RowSpan="2" Width="100" x:Name="pnl2"> <Grid.RowDefinitions> <RowDefinition Height="0.05*"></RowDefinition> <RowDefinition Height="0.15*"></RowDefinition> <RowDefinition Height="0.15*"></RowDefinition> <RowDefinition Height="0.15*"></RowDefinition> <RowDefinition Height="0.15*"></RowDefinition> <RowDefinition Height="0.15*"></RowDefinition> <RowDefinition Height="0.15*"></RowDefinition> <RowDefinition Height="0.05*"></RowDefinition> </Grid.RowDefinitions> <Grid Grid.Row="0"> <Image x:Name="imgUp" Source="/gqfc;component/Photos/up.png" MouseLeftButtonDown="up" Cursor="Hand" ToolTipService.ToolTip="上一个图片"/> </Grid> <Grid Margin="0,10,0,0" Grid.Row="1" > <Image x:Name="img1" Source="/gqfc;component/Photos/fc2.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand" Stretch="Fill" ToolTipService.ToolTip="灌 区 风 采 " /> </Grid> <Grid Margin="0,10,0,0" Grid.Row="2"> <Image x:Name="img2" Source="/gqfc;component/Photos/fc3.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand" Stretch="Fill" ToolTipService.ToolTip="灌 区 风 采 "/> </Grid> <Grid Margin="0,10,0,0" Grid.Row="3"> <Image x:Name="img3" Source="/gqfc;component/Photos/fc4.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand" Stretch="Fill" ToolTipService.ToolTip="灌 区 风 采 "/> </Grid> <Grid Margin="0,10,0,0" Grid.Row="4"> <Image x:Name="img4" Source="/gqfc;component/Photos/fc5.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand" Stretch="Fill" ToolTipService.ToolTip="灌 区 风 采 "/> </Grid> <Grid Margin="0,10,0,0" Grid.Row="5"> <Image x:Name="img5" Source="/gqfc;component/Photos/fc6.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand" Stretch="Fill" ToolTipService.ToolTip="灌 区 风 采 "/> </Grid> <Grid Margin="0,10,0,0" Grid.Row="6"> <Image x:Name="img6" Source="/gqfc;component/Photos/fc7.jpg" MouseLeftButtonDown="ImgClick" Cursor="Hand" Stretch="Fill" ToolTipService.ToolTip="灌 区 风 采 "/> </Grid> <Grid Grid.Row="7"> <Image x:Name="imgDown" Source="/gqfc;component/Photos/down.png" MouseLeftButtonDown="down" Cursor="Hand" ToolTipService.ToolTip="下一个图片"/> </Grid> </Grid> <Border Grid.Column="0" HorizontalAlignment="Center" Grid.Row="0" VerticalAlignment="Center" BorderBrush="#FF54DC18" BorderThickness="1" CornerRadius="5" ToolTipService.ToolTip="灌 区 风 采 " Cursor="Hand" > <Border.Effect> <DropShadowEffect/> </Border.Effect> <Border.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="Black" Offset="0"/> <GradientStop Color="#FFDAB4B4"/> <GradientStop Color="#FFDABDB4" Offset="0.848"/> <GradientStop Color="#FFB4DAC1" Offset="0.525"/> <GradientStop Color="#FFC9C4B9" Offset="0.362"/> </LinearGradientBrush> </Border.Background> <TextBlock x:Name="txtImg" Text="灌 区 风 采 图 片" IsHitTestVisible="False" TextAlignment="Center" FontSize="16" Foreground="#FFAB4444" FontWeight="Bold" Height="23" Width="120"/> </Border> </Grid> </UserControl>

 

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Json; using System.Windows.Media.Effects; using System.Windows.Media.Imaging; using System.Windows.Threading; namespace gqfc { public partial class MainPage : UserControl { /// <summary> /// Json数据源 /// </summary> string imgData = "[{src:'/gqfc;component/Photos/fc1.jpg',name:'灌区风采图片1'},{src:'/gqfc;component/Photos/fc2.jpg',name:'灌区风采图片2'},{src:'/gqfc;component/Photos/fc3.jpg',name:'灌区风采图片3'},{src:'/gqfc;component/Photos/fc4.jpg',name:'灌区风采图片4'},{src:'/gqfc;component/Photos/fc5.jpg',name:'灌区风采图片5'},{src:'/gqfc;component/Photos/fc6.jpg',name:'灌区风采图片6'},{src:'/gqfc;component/Photos/fc7.jpg',name:'灌区风采图片7'},{src:'/gqfc;component/Photos/fc8.jpg',name:'灌区风采图片8'},{src:'/gqfc;component/Photos/fc9.jpg',name:'灌区风采图片9'},{src:'/gqfc;component/Photos/fc10.jpg',name:'灌区风采图片10'},{src:'/gqfc;component/Photos/fc11.jpg',name:'灌区风采图片11'},{src:'/gqfc;component/Photos/fc12.jpg',name:'灌区风采图片12'},{src:'/gqfc;component/Photos/fc13.jpg',name:'灌区风采图片13'}]"; int currentIndex = 0;//当前imgData的索引 int currentImgIndex = 0;//当前第几张小图为阴影区 int _Max = 6;//右侧显示几张小图 List<ImageItem> listSrc = new List<ImageItem>(); private DispatcherTimer _timer; public MainPage() { InitializeComponent(); #region 将Json转化为强类型的List<> JsonValue jv = JsonArray.Parse(imgData); for (int i = 0; i < jv.Count; i++) { listSrc.Add(new ImageItem() { src = jv[i]["src"].ToString().Replace("///", "/").Replace("/"", ""), name = jv[i]["name"].ToString().Replace("///", "/").Replace("/"", "") }); } #endregion currentIndex = 0; currentImgIndex = 0; LoadImage(); #region 启动定时器 _timer = new DispatcherTimer(); _timer.Interval = new TimeSpan(0, 0, 10); _timer.Tick += new EventHandler(_timer_Tick); _timer.Start(); #endregion } void _timer_Tick(object sender, EventArgs e) { down(sender, null); } /// <summary> /// 加载图片 /// </summary> private void LoadImage() { int _start = currentIndex % listSrc.Count; for (int i = 0; i < _Max; i++) { if (_start >= listSrc.Count) { _start = _start % listSrc.Count; } Image img = this.pnl2.FindName("img" + (i + 1).ToString()) as Image; img.Source = new BitmapImage(new Uri(listSrc[_start].src, UriKind.Relative)); if (i == currentImgIndex) { img.Effect = new DropShadowEffect(); imgBig.Source = img.Source; sb_Big.Begin(); txtImg.Text = listSrc[_start].name; } else { img.Effect = null; } _start++; } } /// <summary> /// 点击向上翻时的逻辑处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void up(object sender, MouseButtonEventArgs e) { currentIndex--; if (currentIndex <= 0) { currentIndex = listSrc.Count; } LoadImage(); } /// <summary> /// 点击向下按钮时的逻辑处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void down(object sender, MouseButtonEventArgs e) { currentIndex++; if (currentIndex >= listSrc.Count) { currentIndex = 0; } LoadImage(); } /// <summary> /// 单击右侧小图时,同时步更换大图 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ImgClick(object sender, MouseButtonEventArgs e) { Image imgSmall = sender as Image; imgBig.Source = imgSmall.Source; sb_Big.Begin(); for (int i = 1; i <= 6; i++) { Image img = this.pnl2.FindName("img" + i.ToString()) as Image; if (img == imgSmall) { img.Effect = new DropShadowEffect(); currentImgIndex = i-1;//重新保存新的小图阴影索引 } else { img.Effect = null; } } //确定新的currentIndex for (int i = 0; i < listSrc.Count; i++) { if ((imgSmall.Source as BitmapImage).UriSource == new Uri(listSrc[i].src, UriKind.Relative)) { currentIndex = i; break; } } txtImg.Text = listSrc[currentIndex].name ; } /// <summary> /// 自定义类 /// </summary> public class ImageItem { public string src { set; get; } public string name { set; get; } } private void imgDown_MouseEnter(object sender, MouseEventArgs e) { this._timer.Stop(); } private void imgDown_MouseLeave(object sender, MouseEventArgs e) { this._timer.Start(); } } }

 

相关文章
|
前端开发 Windows
闲来无事,倒腾了一个简单的silverlight视频播放器
#silverlightControlHost { height:407px; width:480px; padding:3px; text-align:center; border:solid 1px #ccc; margin:10px; } 近二日闲来无事,把silverlight的官方文档瞅了瞅,倒腾了一个简单的视频播放器,顺便也测试了下能否播放传说中的h.
1123 0
|
C# 编解码 Windows
WPF播放视频
原文:WPF播放视频 在现在的项目中需要使用到播放视频的功能,本来打算使用VLC来做的。后来发现WPF 4.0之后新增了MediaElement类,可以实现视频播放。 ...
2023 0
|
Web App开发 C# Windows
制作一个简单的WPF图片浏览器
原文:制作一个简单的WPF图片浏览器 注:本例选自MSDN样例,并略有改动。先看效果: 这里实现了以下几个功能:1.  对指定文件夹下所有JPG文件进行预览2.  对选定图片进行旋转3.  对选定图片进行灰度处理4.  对选定图片进行裁切处理5.  无限制的恢复功能6. 类似加入购物车的功能以下来看看其实现过程。
946 0
|
C#
WPF制作的党旗
原文:WPF制作的党旗 --------------------------------------------------------------------------------引用或转载时请保留以下信息:大可山 [MSN:a3news(AT)hotmail.
885 0
|
C# 算法 前端开发
WPF: RenderTransform特效
原文:WPF: RenderTransform特效 WPF中的变形(RenderTransform)类是为了达到直接去改变某个Silverlight对象的形状(比如缩放、旋转一个元素)的目的而设计的,RenderTransform包含的变形属性成员就是专门用来改变Silverlight对象形状的,它可以实现对元素拉伸,旋转,扭曲等效果,同时变形特效也常用于辅助产生各种动画效果。
1342 0
|
索引
WPF3D图片轮播效果
原文:WPF3D图片轮播效果 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37591671/article/details/68059169 1.   效果图: (1)初始化: (2)当点击该图片时,该图片放大 (3)当点击其他图片时,被点击图片移动到屏幕中央,再次点击放大。
1739 0
|
C# 前端开发
WPF特效-拼图游戏
原文:WPF特效-拼图游戏 此文主要描述我实现碎片化的便捷过程。 步骤1:    选取参考图如下(百度图库搜的): 步骤2:    根据效果图或者模型构建贝塞尔曲线,为了方便查看效果,可以设置控制点,Console.WriteLine或直接UI上显示的方式记录最终坐标,效果图如:    上图中的红线,为两条贝塞尔曲线,左侧的位置我已经通过控制点的方式调好。
1395 0
|
开发框架 前端开发 .NET