Window 8.1 计时器功能及图片切换

时间:2023-02-04 05:16:45
  <Canvas Margin="450,0" Width="795" Grid.Column="1">
<Image Margin="15,15,15,15" Width="64" Height="64" Source="image/Content/clock.png"></Image>
<TextBlock Text="{Binding TimerContext}" FontSize="45" Height="50" Width="170" Margin="90,20,0,0" />
<Button Margin="280,20" FontSize="28" Width="180" Height="50" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Command="{Binding StartAndPauseCommand}" >
<Button.Background>
<SolidColorBrush Color="#FFE0F1F9"/>
</Button.Background>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="44"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="{Binding StartAndPauseButtonImage}" Width="40" Height="35" Margin="-1,-1,5,-2"></Image>
<TextBlock Grid.Column="1" Margin="10,-5" Foreground="#FF00C5A2" Text="{Binding ImageOperationText}" Width="100" />
</Grid>
</Button>
</Button>
  private TimeSpan _timeSpan;
void dispatcherTimer_Tick(object sender, object e)
{ _timeSpan = _timeSpan.Add(new TimeSpan(, , ));
TimerContext = _timeSpan.ToString(); }
public string ImageOperationText
{
get { return imageOperationText; }
set
{
if (imageOperationText == value) return;
imageOperationText = value;
base.RaisePropertyChanged("ImageOperationText");
}
} private string timerContext = TicketContentConst.TimerContextText;
public string TimerContext
{ get { return timerContext; }
set
{
if (timerContext == value) return;
timerContext = value;
base.RaisePropertyChanged("TimerContext");
}
}
#region Command
private RelayCommand startAndPauseCommand;
public RelayCommand StartAndPauseCommand
{
get
{
if (startAndPauseCommand == null)
startAndPauseCommand = new RelayCommand(startAndPause);
return startAndPauseCommand;
}
}
#endregion
/// <summary>
/// dispatcherTimer Operations
/// </summary>
private void startAndPause()
{
if (!isTimerStart)
{ dispatcherTimer.Start();
StartAndPauseButtonImage = TicketContentConst.PauseImagePath;
ImageOperationText = TicketContentConst.PauseContentText;
if (startDateTime == DateTime.MinValue)
{
startDateTime = DateTime.Now;
} }
else
{ dispatcherTimer.Stop();
ImageOperationText = TicketContentConst.StartContentText;
StartAndPauseButtonImage = TicketContentConst.StartImagePath; }
isTimerStart = !isTimerStart; }
/// <summary>
/// 计时器设置
/// </summary>
public void DispatcherTimerSetup()
{ dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(, , );
_timeSpan = new TimeSpan(); }
</Canvas>