如何显示具有时差的文本?(Monodevelopment C#)



我从 MonoDevelopment 制作了一个 GTK# 2.0 项目,并修改了 MainWindow.cs如下所示;

using System;
using System.Threading;
using Gtk;
public partial class MainWindow : Gtk.Window
{
    public MainWindow() : base(Gtk.WindowType.Toplevel)
    {
        Build();
    }
    protected void OnDeleteEvent(object sender, DeleteEventArgs a)
    {
        Application.Quit();
        a.RetVal = true;
    }
    protected void OnButtonClicked(object sender, EventArgs e)
    {
        textview.Buffer.Text = "Hello, world!";
        Thread.Sleep(2500);
        textview.Buffer.Text += Environment.NewLine;
        textview.Buffer.Text += "Hello, world!";
    }
}

我的意图是:首先显示"Hello,world!",2秒半后,下一行显示另一个"Hello,world!"。

但是,当我按下按钮时实际发生了什么:两个"你好,世界"在 2 秒半之后同时显示。

那么如何显示两条时差线呢?

请改用Task.Delay。您需要从 async 中标记调用它的方法。

await Task.Delay(3000);

@Doruk

using System;
using System.Threading;
using Gtk;
public partial class MainWindow : Gtk.Window
{
    public MainWindow() : base(Gtk.WindowType.Toplevel)
    {
        Build();
    }
    protected void OnDeleteEvent(object sender, DeleteEventArgs a)
    {
        Application.Quit();
        a.RetVal = true;
    }
    protected async void OnButtonClicked(object sender, EventArgs e)
    {
        textview.Buffer.Text = "Hello, world!";
        textview.Buffer.Text += Environment.NewLine;
        await Task.Delay(2500);
        textview.Buffer.Text += "Hello, world!";
    }
}

最新更新