本文介绍了如何在WPF中获取数字以在屏幕上向前计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的就是向我5岁的女儿展示屏幕上的数字如何计数.

All I wanted to do was show my 5-year-old daughter how a number can count forward on the screen.

这将等待135秒,然后显示"135".

This waits 135 seconds and then displays "135".

我需要更改什么,以便它显示计数的数字?

XAML:

<Window x:Class="TestCount234.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="768" Width="1024">
    <StackPanel>
        <TextBlock
            HorizontalAlignment="Center"
            FontSize="444" x:Name="TheNumber"/>
    </StackPanel>
</Window>

隐藏代码:

using System.Windows;
using System.Threading;

namespace TestCount234
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Window1_Loaded);
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i <= 135; i++)
            {
                TheNumber.Text = i.ToString();
                Thread.Sleep(1000);
            }
        }
    }
}

推荐答案

对于这样的快速项目,您可以使用计时器:

For a quickie project like this, you could use a timer:

private DispatcherTimer timer;
private int count = 0;

public Window1()
{
    InitializeComponent();
    this.timer = new DispatcherTimer();
    this.timer.Interval = TimeSpan.FromSeconds(1);
    this.timer.Tick += new EventHandler(timer_Tick);
    this.timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    this.textBox1.Text = (++count).ToString();
}

这篇关于如何在WPF中获取数字以在屏幕上向前计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:05