Silverlight开发实践--My Album(源)
2009-04-08 12:01:20 来源:WEB开发网其他图片对象的代码请参照源码。
最后的工作就是利用C#加载图片,以及编写鼠标移入移出和单击响应的事件。
public string AlbumTitle = "Brian's Constellation Album";//定义页面题目
public string[] ImageArray = new string[12];//定义图片路径数组
private bool zoomFlag = false;//判断图片放大还是缩小
public Page()
{
InitializeComponent();
LoadImages();
}
private void InitImagePath()
{
ImageArray[0] = "Image/1.jpg";
ImageArray[1] = "Image/2.jpg";
ImageArray[2] = "Image/3.jpg";
ImageArray[3] = "Image/4.jpg";
ImageArray[4] = "Image/5.jpg";
ImageArray[5] = "Image/6.jpg";
ImageArray[6] = "Image/7.jpg";
ImageArray[7] = "Image/8.jpg";
ImageArray[8] = "Image/9.jpg";
ImageArray[9] = "Image/10.jpg";
ImageArray[10] = "Image/11.jpg";
ImageArray[11] = "Image/12.jpg";
}
private void LoadImages()
{
InitImagePath();
txtTitle.Text = AlbumTitle;
for (int i = 0; i <=ImageArray.Length-1; i++)
{
if(!string.IsNullOrEmpty(ImageArray[i]))
{
string namestr = "album" + i.ToString();
Canvas currentCanves = (Canvas)this.FindName(namestr);
currentCanves.MouseEnter +=new MouseEventHandler(mouseEnter);
currentCanves.MouseLeave += new MouseEventHandler(mouseLeave);
currentCanves.MouseLeftButtonDown += new MouseButtonEventHandler(PicZoomIn);
currentCanves.Opacity = 1;
currentCanves.Visibility = Visibility.Visible;
namestr = "Image" + i.ToString();
((Image)this.FindName(namestr)).Source = new BitmapImage(new Uri(ImageArray[i], UriKind.Relative));
}
}
}
protected void mouseEnter(object sender, MouseEventArgs e)
{
//设置ZIndex为0,使其不会被其他画布遮住
for (int i = 0; i < ImageArray.Length; i++)
{
string namestr = "album" + i.ToString();
((Canvas)this.FindName(namestr)).SetValue(Canvas.ZIndexProperty, 0);
}
string imagepostion = ((Canvas)sender).Name.Substring(5);
string namestring = "mouseEnter" + imagepostion.ToString();
((Storyboard)this.FindName(namestring)).Begin();//调用故事板的Begin方法开始动画
}
protected void mouseLeave(object sender, EventArgs e)
{
string imagepostion = ((Canvas)sender).Name.Substring(5);//获取5个字符后的数字
if (zoomFlag)
{
string namestring = "PicZoomOut" + imagepostion;
((Storyboard)this.FindName(namestring)).Begin();
namestring = "mouseLeave" + imagepostion;
((Storyboard)this.FindName(namestring)).Begin();
zoomFlag = false;
}
else
{
string namestring = "mouseLeave" + imagepostion;
((Storyboard)this.FindName(namestring)).Begin();
}
}
protected void PicZoomIn(object sender, MouseEventArgs e)
{
string imagepostion = ((Canvas)sender).Name.Substring(5);
if (zoomFlag)
{
string namestring = "PicZoomOut" + imagepostion;
((Storyboard)this.FindName(namestring)).Begin();
zoomFlag = false;
}
else
{
string namestring = "PicZoomIn" + imagepostion;
((Storyboard)this.FindName(namestring)).Begin();
((Canvas)sender).SetValue(Canvas.ZIndexProperty, 1);
zoomFlag = true;
}
}
实现的原理很简单,比较适合学习SL的新手。仅供学习参考。
最后留个问题:我打算实现图片从中心处变大,但是没有成功,只能用改变长度和宽度的方法,希望SL高手指教。
Tags:Silverlight 开发 实践
编辑录入:爽爽 [复制链接] [打 印]- ››开发Android 日历教程
- ››开发学院总结 Win 8实用技巧大全
- ››开发学院原创教程:把win8的IE10放桌面上方法(非...
- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››开发者眼中的Windows Phone和Android
- ››开发学院教你用SQL 语句最快速清空MySQL 数据表的...
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››开发一个自己的HTML在线编辑器(一)
更多精彩
赞助商链接