Window 8.1 计时器功能及图片切换
1 <Canvas Margin="450,0" Width="795" Grid.Column="1"> 2 <Image Margin="15,15,15,15" Width="64" Height="64" Source="image/Content/clock.png"></Image> 3 <TextBlock Text="{Binding TimerContext}" FontSize="45" Height="50" Width="170" Margin="90,20,0,0" /> 4 <Button Margin="280,20" FontSize="28" Width="180" Height="50" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Command="{Binding StartAndPauseCommand}" > 5 <Button.Background> 6 <SolidColorBrush Color="#FFE0F1F9"/> 7 </Button.Background> 8 <Grid> 9 <Grid.ColumnDefinitions> 10 <ColumnDefinition Width="44"/> 11 <ColumnDefinition Width="*"/> 12 </Grid.ColumnDefinitions> 13 <Image Source="{Binding StartAndPauseButtonImage}" Width="40" Height="35" Margin="-1,-1,5,-2"></Image> 14 <TextBlock Grid.Column="1" Margin="10,-5" Foreground="#FF00C5A2" Text="{Binding ImageOperationText}" Width="100" /> 15 </Grid> 16 </Button> 17 </Button> 18
1 private TimeSpan _timeSpan; 2 void dispatcherTimer_Tick(object sender, object e) 3 { 4 5 _timeSpan = _timeSpan.Add(new TimeSpan(0, 0, 1)); 6 TimerContext = _timeSpan.ToString(); 7 8 } 9 public string ImageOperationText 10 { 11 get { return imageOperationText; } 12 set 13 { 14 if (imageOperationText == value) return; 15 imageOperationText = value; 16 base.RaisePropertyChanged("ImageOperationText"); 17 } 18 } 19 20 21 private string timerContext = TicketContentConst.TimerContextText; 22 public string TimerContext 23 { 24 25 get { return timerContext; } 26 set 27 { 28 if (timerContext == value) return; 29 timerContext = value; 30 base.RaisePropertyChanged("TimerContext"); 31 } 32 } 33 #region Command 34 private RelayCommand startAndPauseCommand; 35 public RelayCommand StartAndPauseCommand 36 { 37 get 38 { 39 if (startAndPauseCommand == null) 40 startAndPauseCommand = new RelayCommand(startAndPause); 41 return startAndPauseCommand; 42 } 43 } 44 #endregion 45 /// <summary> 46 /// dispatcherTimer Operations 47 /// </summary> 48 private void startAndPause() 49 { 50 if (!isTimerStart) 51 { 52 53 dispatcherTimer.Start(); 54 StartAndPauseButtonImage = TicketContentConst.PauseImagePath; 55 ImageOperationText = TicketContentConst.PauseContentText; 56 if (startDateTime == DateTime.MinValue) 57 { 58 startDateTime = DateTime.Now; 59 } 60 61 } 62 else 63 { 64 65 dispatcherTimer.Stop(); 66 ImageOperationText = TicketContentConst.StartContentText; 67 StartAndPauseButtonImage = TicketContentConst.StartImagePath; 68 69 70 } 71 isTimerStart = !isTimerStart; 72 73 } 74 /// <summary> 75 /// 计时器设置 76 /// </summary> 77 public void DispatcherTimerSetup() 78 { 79 80 dispatcherTimer = new DispatcherTimer(); 81 dispatcherTimer.Tick += dispatcherTimer_Tick; 82 dispatcherTimer.Interval = new TimeSpan(0, 0, 1); 83 _timeSpan = new TimeSpan(); 84 85 }
</Canvas>