c# wpf定时器的一种用法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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));
        }
    }
}

  

以上是关于c# wpf定时器的一种用法的主要内容,如果未能解决你的问题,请参考以下文章

C# WPF GridControl用法举例

C# WPF TreeView用法实例解析

C# WPF开源控件库HandyControl用法举例

C# WPF TabControl用法指南(精品)

C#中简单的问题,关于WPF程序的计时器控件。

C# WPF 坦克大战