哈,··又來了~
1、建立一個按鈕,content 寫上Play Game (這個隨你,你寫,坑爹吖·· 也行!)
2、建立一個Canvas容器,顯示那個一閃一閃的,,,(為啥不是Image,容器自由度比較大嘛···哈哈)
3、接著開始想那個一閃一閃的,又在Blend 畫不就行了,呵呵·····沒錯··但這個一閃一閃的···要到後來玩的時候要用的,···都這樣畫上去顯然不科學額,so,這個一閃···還是自己用代碼敲上去比較合理,嗯, 其實應該還有更好的,··不過我菜。這能寫成這樣子了。
廢話少說,。看下面的
建一個Common類,哈~~~
/// <summary> /// 擷取動畫 /// </summary> /// <param name="Index"></param> /// <param name="Max"></param> /// <param name="url"></param> /// <param name="style">1:迴圈 0:不迴圈</param> /// <returns></returns> public static ImageSource GetGif( ref int Index, ref int Max, ref bool isHit,string url,int style) { ImageSource source = null; if (Index < Max) { source = Common.GetImgSource(url); isHit = true; Index++; } else { if (style == 1) { //迴圈 Index = 0; return null; } else { //不迴圈 isHit = false; return null; } } return source; } /// <summary> /// 擷取圖片來源 /// </summary> /// <param name="url"></param> /// <returns></returns> public static ImageSource GetImgSource(string url) { ImageSource source = new BitmapImage(GetUri(url)); return source; } /// <summary> /// 擷取資源 /// </summary> /// <param name="url"></param> /// <returns></returns> public static Uri GetUri(string url) { Uri uri = new Uri(url, UriKind.Relative); return uri; }
WPF,silverlight,讀取東東都是資源額,···Uri ,so,要改變一下以前··直接Path路徑思想,······(如果你直接用Path ···他會彈出拒絕訪問··等錯誤資訊額··)
5、在UserControls檔案夾裡面建立一個···命名為Hit 的使用者控制項, 嗯嗯,
6、設計這個Hit.xaml,其實沒啥的··就是定義他的大小和添加一個Image元素。後台才是重點,
<UserControl x:Class="SilverlightMusicHit.Hit" 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" d:DesignHeight="135" d:DesignWidth="135"> <Image Grid.Column="1" Height="135" HorizontalAlignment="Left" Name="imgHit" Stretch="Fill" VerticalAlignment="Top" Width="135" /></UserControl>
7、編輯後台
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.Media.Imaging;namespace SilverlightMusicHit{ public partial class Hit : UserControl { #region 變數 /// <summary> /// Hit顯示索引,0 bad,1 perfect /// </summary> public int hitDisIndex = 0; /// <summary> /// 顯示類型,1 迴圈,0 不迴圈 /// </summary> public int HitStyle = 0; /// <summary> /// Hit點擊索引 /// </summary> public bool isHit = false; /// <summary> /// Hit顯示速度 /ms /// </summary> private int speed = 30; /// <summary> /// Hit顯示圖片數量 /// </summary> private int hitMax = 6; /// <summary> /// Hit顯示列表 /// </summary> private string[] hitUrl = { "Image/PicHit/gauge_bad_", "Image/PicHit/gauge_perfect_" }; /// <summary> /// 定時器 /// </summary> private Storyboard _timer; /// <summary> /// Hit圖片索引 /// </summary> private int hitIndex = 0; #endregion public Hit() { InitializeComponent(); //建立定時器 _timer = new Storyboard(); _timer.Duration = new Duration(TimeSpan.FromMilliseconds(speed)); _timer.Completed += new EventHandler(_timer_Completed); _timer.Begin(); } void _timer_Completed(object sender, EventArgs e) { string url = string.Format("../{0}{1}{2}", hitUrl[hitDisIndex], hitIndex, ".png"); imgHit.Source = Common.GetGif(ref hitIndex, ref hitMax, ref isHit, url, HitStyle); if (isHit) _timer.Begin(); else _timer.Stop(); } }}
應該大部份都看得懂吧,···重點是···StoryBoard 這個故事板, 查看MSDN就會發現了,··哈··還是要善於看MSDN吖··很好很強大
StoryBoard
為容器的子動畫提供對象和屬性目標資訊的容器時間軸。
| Duration |
擷取或設定此時間軸播放的時間長度,而不是計數重複。這是一個依賴項屬性。 (繼承自 Timeline。) |
| Completed |
當此時間軸完全播放完畢時發生:它將不再進入其活動周期。 (繼承自 Timeline。) |
好了··HIT 一閃一閃完成了,··
按F5運行, 吖。 。 沒有?
別著急,你沒錯,··其實還沒做好的······ (*^__^*) 嘻嘻
8、回到MainPage編輯後台,在他建構函式裡面··寫上以下代碼
public MainPage() { InitializeComponent(); //建立Hit Hit hit = new Hit(); Random rand = new Random(); hit.hitDisIndex = rand.Next(0, 1); hit.HitStyle = 1; this.Hit.Children.Add(hit); }
再按F5,額··還沒有? 呵呵,··沒有圖片,當然沒有啦,···
9、在Image檔案夾裡面再建立個PicHit檔案夾,添加以片
http://www.vdisk.cn/down/index/8805162A4337
呵呵··現在按F5,應該行了吧。嗯嗯··
name:5+x
參考文章與書籍:
WPF葵花寶典
silverlight2.0 開發技術與精粹