C# 在WPF中,如何在屏幕上获得向前计数的数字?

C# 在WPF中,如何在屏幕上获得向前计数的数字?,c#,multithreading,C#,Multithreading,我想做的就是向我5岁的女儿展示一个数字如何在屏幕上向前计数 等待135秒,然后显示“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" Tit

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

等待135秒,然后显示“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);
            }
        }
    }
}

代码隐藏:

<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);
            }
        }
    }
}
使用System.Windows;
使用系统线程;
命名空间TestCount234
{
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
已加载+=新路由EventHandler(Window1_已加载);
}
无效窗口1_已加载(对象发送器,路由目标e)
{

对于(int i=0;i如果希望在任务运行时更新UI(并保持响应),则需要使用单独的线程,例如使用
BackgroundWorker

下面是一个如何工作的示例:

BackgroundWorker _backgroundWorker = new BackgroundWorker();

...

// Set up the Background Worker Events
_backgroundWorker.DoWork += _backgroundWorker_DoWork;
backgroundWorker.RunWorkerCompleted += 
    _backgroundWorker_RunWorkerCompleted;

// Run the Background Worker
_backgroundWorker.RunWorkerAsync(5000);

...

// Worker Method
void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // Do something
}

// Completed Method
void _backgroundWorker_RunWorkerCompleted(
    object sender, 
    RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled)
    {
        statusText.Text = "Cancelled";
    }
    else if (e.Error != null) 
    {
        statusText.Text = "Exception Thrown";
    }
    else 
    {
        statusText.Text = "Completed";
    }
}

如果希望在任务运行时更新UI(并保持响应),则需要使用单独的线程,例如使用
BackgroundWorker

下面是一个如何工作的示例:

BackgroundWorker _backgroundWorker = new BackgroundWorker();

...

// Set up the Background Worker Events
_backgroundWorker.DoWork += _backgroundWorker_DoWork;
backgroundWorker.RunWorkerCompleted += 
    _backgroundWorker_RunWorkerCompleted;

// Run the Background Worker
_backgroundWorker.RunWorkerAsync(5000);

...

// Worker Method
void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // Do something
}

// Completed Method
void _backgroundWorker_RunWorkerCompleted(
    object sender, 
    RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled)
    {
        statusText.Text = "Cancelled";
    }
    else if (e.Error != null) 
    {
        statusText.Text = "Exception Thrown";
    }
    else 
    {
        statusText.Text = "Completed";
    }
}

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

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

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

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

谢谢,这很有效,在5岁孩子的注意力范围内,谢谢!谢谢,这很有效,在5岁孩子的注意力范围内,谢谢!