Windows Phone 自定义控件 定时炸弹实例

炸弹XAML

01:  <UserControl x:Class="CrazyBomb.Bomb"
02:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
03:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
04:      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
05:      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
06:      mc:Ignorable="d"
07:      d:DesignHeight="100" d:DesignWidth="100"  >
08:      <Image Name="imgBomb" Stretch="Fill"  Width="100" Height="100"  Source="/CrazyBomb;component/Images/SleepState.png" MouseLeftButtonDown="imgBomb_MouseLeftButtonDown" />
09:  </UserControl>
10:  
11:  

炸弹CS

001:  using System.Windows.Media.Imaging;
002:  using System.Windows.Threading;
003:  
004:  namespace CrazyBomb
005:  {
006:      public partial class Bomb : UserControl
007:      {
008:          protected BombState state;
009:          public BombState State
010:          {
011:              get { return state;}
012:          }
013:  
014:          public delegate void PunishHandler(Bomb bomb);
015:          public PunishHandler Punish; //委托成员
016:          public delegate void OverGameHandler(Bomb bomb);
017:          public OverGameHandler OverGame; //委托成员
018:  
019:          protected TimeSpan fireTime;
020:          protected TimeSpan delayTime=new TimeSpan(0,0,5);//五秒
021:          protected DispatcherTimer fireTimer; //时间起
022:          protected DispatcherTimer delayTimer; //时间器
023:          protected static Random random = new Random(); //随机器
024:  
025:          public Bomb()
026:          {
027:              InitializeComponent();
028:  
029:              fireTimer = new DispatcherTimer();
030:              fireTimer.Tick += new EventHandler(fireTimer_Tick); //每一次心跳的事件
031:  
032:              delayTimer = new DispatcherTimer();
033:              delayTimer.Tick += new EventHandler(delayTimer_Tick);
034:              delayTimer.Interval = delayTime; //指定间隔为5秒
035:  
036:              GoSleepState();
037:          }
038:  
039:          //转到4种状态
040:          public void GoSleepState()
041:          {
042:              state = BombState.SleepSate;
043:              imgBomb.Source = new BitmapImage(new Uri("Images/SleepState.png",UriKind.RelativeOrAbsolute));
044:          }
045:          public void GoActiveState()
046:          {
047:              state = BombState.ActiveState;
048:              imgBomb.Source = new BitmapImage(new Uri("Images/ActiveState.png", UriKind.RelativeOrAbsolute));
049:  
050:              delayTimer.Stop();
051:              int seconds =random.Next(2,10);//随机等待秒数
052:              fireTimer.Interval = new TimeSpan(0, 0, seconds);
053:              fireTimer.Start();
054:          }
055:  
056:          public void GoDelayState()
057:          {
058:              state = BombState.DelayState;
059:              imgBomb.Source = new BitmapImage(new Uri("Images/DelayState.png", UriKind.RelativeOrAbsolute));
060:  
061:              fireTimer.Stop(); //时间器停止
062:              delayTimer.Start(); //点火时间器开始,5秒一次
063:          }
064:  
065:          public void GoBombingState()
066:          {
067:              state = BombState.BombingState;
068:              //换图片
069:              imgBomb.Source = new BitmapImage(new Uri("Images/BombingState.png", UriKind.RelativeOrAbsolute));
070:  
071:              delayTimer.Stop();
072:          }
073:  
074:          //计时器命中
075:          protected void fireTimer_Tick(object sender, EventArgs e)
076:          {
077:              //如果游戏启动
078:              if (App.CurrentState == GameState.Running )
079:              {
080:                  GoDelayState();
081:              }
082:          }
083:          //点燃计时器
084:          protected void delayTimer_Tick(object sender, EventArgs e)
085:          {
086:              if (App.CurrentState == GameState.Running)
087:              {
088:                  //如果点燃时间到了,就结束游戏了
089:                  GoBombingState();
090:                  //调用结束游戏委托
091:                  OverGame(this);
092:              }
093:          }
094:  
095:          //单击事件
096:          private void imgBomb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
097:          {
098:              switch (state)
099:              {
100:                  case BombState.SleepSate://如果是等待,则变成激活
101:                      GoActiveState();
102:                      break;
103:                  case BombState.ActiveState://如果是激活,调用委托方法
104:                      Punish(this);
105:                      break;
106:                  case BombState.DelayState://如果是点燃,则回归激活
107:                      GoActiveState();
108:                      break;
109:              }
110:          }
111:  
112:  
113:      }
114:  }
原文地址:https://www.cnblogs.com/finehappy/p/2063547.html