SilverLight入门实例(一)
生活随笔
收集整理的這篇文章主要介紹了
SilverLight入门实例(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
大家好,在學習Silverlight的朋友門,如果你剛剛開始學習銀光,那就和我一起來探討這個神奇的東東,或者也可以加我的QQ:16853655,注明“博客園:銀光”
?
我們先開始做一個圖片滾動展覽店
第一步,相信各位都會操作吧,添加一個Silverlight的應用程序項目
?
在Silverlight程序項目下(非WEB項目哦),添加一下兩個文件
建立“ShopShow.xaml” 控件文件,代碼如下:
?
ShopShow.xaml ?1?<UserControl?x:Class="SilverlightDemo.ShopShow"?2?????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
?3?????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
?4??????????????Width="1280"?Height="800"
?5??????????????>
?6?????<Grid>
?7?????????<Grid.Background>
?8?????????????<ImageBrush?ImageSource="http://localhost:2149/Images/bg.jpg"?Stretch="Fill"?/>
?9?????????</Grid.Background>
10?????????<Image?x:Name="shower"?Width="400"?Height="300"?Stretch="Fill"?Visibility="Collapsed">
11?????????????<Image.Effect>
12?????????????????<DropShadowEffect?Color="Aqua"?BlurRadius="10"?Opacity="0.8"?ShadowDepth="0"/>
13?????????????</Image.Effect>
14?????????</Image>
15?????????<Canvas?x:Name="moveCanvas"?Margin="250?160?0?0"></Canvas>
16?????????<StackPanel?Orientation="Horizontal"?Margin="500?500?0?0">
17?????????????<Button?Width="50"?Height="30"?Content="Play"?Margin="10"?x:Name="btnStart"?Click="btnStart_Click"></Button>
18?????????????<Button?Width="50"?Height="30"?Content="Stop"?Margin="10"?x:Name="btnStop"?Click="btnStop_Click"></Button>
19?????????</StackPanel>
20?????</Grid>
21?</UserControl>
22?
?
?
?
ShopShop.xaml.cs 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.Windows.Threading;
using?System.Windows.Media.Imaging;
namespace?SilverlightDemo
{
????public?partial?class?ShopShow?:?UserControl
????{
????????private?double?centerX?=?400;
????????private?double?centerY?=?300;
????????private?double?width?=?400;
????????private?double?height?=?60;
????????private?double?degree?=?0;//度數值
????????List<ShopItem>?objList?=?new?List<ShopItem>();//項集合類
????????private?double?itemWidth?=?160;
????????private?double?itemHeight?=?80;
????????private?double?count?=?14;
????????private?double?currentOpacity?=?0;
????????private?DispatcherTimer?timer;
????????public?ShopShow()
????????{
????????????InitializeComponent();
????????????this.Loaded?+=new?RoutedEventHandler(ShopShow_Loaded);
????????}
????????private?void?ShopShow_Loaded(object?sender,?RoutedEventArgs?e)
????????{
????????????this.timer?=?new?DispatcherTimer();
????????????for?(var?i?=?1;?i?<=?this.count;?i++)
????????????{
????????????????//實例化用戶控件
????????????????ShopItem?myShopItem?=?new?ShopItem();
????????????????Image?myImage?=?myShopItem.obj;
????????????????//加載唱片圖片
????????????????Uri?myUri?=?new?Uri(String.Format("http://localhost:2149/Images/album{0}.jpg",?i));
????????????????BitmapImage?bitmap?=?new?BitmapImage(myUri);
????????????????myImage.Source?=?bitmap;
????????????????//綁定控件事件
????????????????myImage.MouseEnter?+=new?MouseEventHandler(myImage_MouseEnter);
????????????????myImage.MouseLeave?+=new?MouseEventHandler(myImage_MouseLeave);
????????????????myImage.MouseLeftButtonDown?+=new?MouseButtonEventHandler(myImage_MouseLeftButtonDown);
????????????????//添加到用戶控件里
????????????????this.objList.Add(myShopItem);
????????????????moveCanvas.Children.Add(myShopItem);
????????????}
????????????timer.Tick?+=?new?EventHandler(timer_Tick);
????????????TimeSpan?sp?=?new?TimeSpan(0,?0,?0,?0,?10);
????????????timer.Interval?=?sp;
????????????timer.Start();
????????}
????????public?void?myImage_MouseLeftButtonDown(object?sender,?MouseButtonEventArgs?e)
????????{
????????????Image?img?=?sender?as?Image;
????????????shower.Visibility?=?Visibility.Visible;
????????????shower.Source?=?img.Source;
????????}
????????public?void?myImage_MouseEnter(object?sender,?MouseEventArgs?e)
????????{
????????????timer.Stop();
????????????Image?img?=?sender?as?Image;
????????????currentOpacity?=?img.Opacity;
????????????img.Opacity?=?1;
????????}
????????public?void?myImage_MouseLeave(object?sender,?MouseEventArgs?e)
????????{
????????????timer.Start();
????????????Image?img?=?sender?as?Image;
????????????img.Opacity?=?currentOpacity;
????????}
????????public?void?timer_Tick(object?sender,?EventArgs?e)
????????{
????????????StartMove();
????????}
????????private?void?StartMove()
????????{
????????????for?(var?i?=?0;?i?<?objList.Count;i++?)
????????????{
????????????????//根據控件數量總數和周圓計算一個平均值
????????????????var?tmp?=?(this.degree?+?(360?/?this.count?*?i))?%?360;
????????????????tmp?=?tmp?*?Math.PI?/?180;
????????????????var?posX?=?(this.width)?*?Math.Sin(tmp);//更新X坐標
????????????????var?posY?=?(this.height)?*?Math.Cos(tmp);//更新Y坐標
????????????????ShopItem?obj?=?this.objList[i];
????????????????//根據高寬計算縮放比例
????????????????double?scale?=?(2?*?this.height?-?posY)?/?(3?*?this.height?+?this.itemHeight?/?2);
????????????????Canvas.SetLeft(obj,?centerX?+?posX?-?(itemWidth?/?2)?*?scale);
????????????????Canvas.SetTop(obj,?centerY?-?posY?-?(itemHeight?/?2)?*?scale);
????????????????Canvas.SetZIndex(obj,?int.Parse(Math.Ceiling(count?*?scale).ToString()));
????????????????//創建并應用變形屬性
????????????????ScaleTransform?st?=?new?ScaleTransform();
????????????????st.ScaleX?=?scale;
????????????????st.ScaleY?=?scale;
????????????????obj.RenderTransform?=?st;
????????????????obj.Opacity?=?scale;
????????????}
????????????this.degree?=?this.degree?-?0.3;
????????}
????????private?void?btnStart_Click(object?sender,?RoutedEventArgs?e)
????????{
????????????timer.Start();
????????}
????????private?void?btnStop_Click(object?sender,?RoutedEventArgs?e)
????????{
????????????timer.Stop();
????????}
????}
}
?
?
建立 “ShopItem.xaml” 控件文件,代碼如下:
?
<UserControl?x:Class="SilverlightDemo.ShopItem"????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"?
????Width="135"?Height="135">
????<Grid?x:Name="LayoutRoot"?Background="White">
????????<Image?x:Name="obj"
???????????Width="135"
???????????Height="135"
???????????Stretch="Fill"/>
????</Grid>
</UserControl>
?
后臺CS文件無需添加代碼。
?
建立以上兩個文件后,在App.xaml.cs的啟動函數里修改如下:
?
????????private?void?Application_Startup(object?sender,?StartupEventArgs?e)????????{
????????????this.RootVisual?=?new?ShopShow();
????????}
?
?
然后在準備一個背景圖片和14個類似海報的圖片。
?
好了,開始運行去吧,別忘記先編譯Silverlight程序項目后,再測試WEB瀏覽!
轉載于:https://www.cnblogs.com/rjf1979/archive/2010/09/07/1820978.html
總結
以上是生活随笔為你收集整理的SilverLight入门实例(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ScrollView’s handy t
- 下一篇: JAVA中深拷贝与浅拷贝(在网上找到的)