我的程序需要显示一些文本以显示x秒。
问题在于,仅在完成时间跨度检查之后才显示文本。
这是我的代码:

        // Clicks button to show texts

        //Displays text wanted basicly Text.Visibility =Visibility.Visible;
        DisplayWords();

        //Waits x amount of seconds before hidden them
        int nbOfSecondsToWait = Convert.ToInt32(SecondAffichage.Value);
        DateTime timeNow;
        timeNow = DateTime.Now;
        TimeSpan timePassed = (DateTime.Now - timeNow);
        TimeSpan timePassedWanted = new TimeSpan(0, 0, nbOfSecondsToWait);
        while (timePassed < timePassedWanted)
        {
            timePassed = DateTime.Now - timeNow;

        }

        //Hide texts


我的文本仅在时间跨度检查后显示,然后立即被隐藏

最佳答案

在异步方法中使用Task.Delay

public async Task ShowText()
{
    DisplayWords();

    int nbOfSecondsToWait = Convert.ToInt32(SecondAffichage.Value);

    await Task.Delay(TimeSpan.FromSeconds(nbOfSecondsToWait));

    //Hide texts
}

关于c# - WPF图片仅在时间跨度后更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53128317/

10-12 20:47