c# wpf定时器的一种用法

1、xaml页面
<Window x:Class="EssentialWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button
Name="_button1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="9pt">
Hello World
</Button>
</Grid>
</Window> 2、后台 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading; namespace EssentialWPF
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
long _start;
public MainWindow()
{
InitializeComponent(); DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(50);
_start = Environment.TickCount;
timer.Tick += timer_Tick;
timer.IsEnabled = true;
} void timer_Tick(object sender, EventArgs e)
{
long elapsed = Environment.TickCount - _start;
if (elapsed >= 5000)
{
_button1.FontSize = 18.0;
((DispatcherTimer)sender).IsEnabled = false;
return;
}
_button1.FontSize = 9.0 + (9.0 / (5000.0 / elapsed));
}
}
}

  

上一篇:UITabBar,UINavigationBar的布局和隐藏问题


下一篇:JavaFX--第3天窗口布局