码迷,mamicode.com
首页 > Windows程序 > 详细

c# wpf定时器的一种用法

时间:2016-09-05 19:09:03      阅读:738      评论:0      收藏:0      [点我收藏+]

标签:

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));
        }
    }
}

  

c# wpf定时器的一种用法

标签:

原文地址:http://www.cnblogs.com/meng01/p/5843100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!