向文本块 WPF 添加倒数计时器

Adding a countdown timer to textblock WPF

我想使用 wpf 将用于在 CMD 上工作的倒数计时器实现到文本块中。

我不知道如何将该代码实现到文本块中。

  DateTime daysLeft1 = DateTime.Parse("1/02/2022 12:00:01 AM");
                DateTime startDate1 = DateTime.Now;


                TimeSpan t = daysLeft1 - startDate1;
                string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds until launch.",
                    t.Days, t.Hours, t.Minutes, t.Seconds);

感谢您的帮助! .

编辑:下面的代码似乎有效,但我有错误 CS0120

“非静态字段、方法或 属性 需要对象引用 'TextBlock.Text'”

public partial class justdancetrailer : Window
{
    private readonly DispatcherTimer Timer = new DispatcherTimer();
    private readonly DateTime daysLeft;

    public justdancetrailer()
    {
        InitializeComponent();

        daysLeft = DateTime.Parse("1/02/2022 12:00:00 AM");

        Timer.Interval = TimeSpan.FromSeconds(1.0); ;
        Timer.Tick += Timer_Tick;
        Timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        TimeSpan t = daysLeft.Subtract(DateTime.Now);
        if (t.Seconds >= 0)
        {
            string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds until launch.",
                t.Days, t.Hours, t.Minutes, t.Seconds);

            



            TextBlock.Text = countDown;

            daysLeft.AddSeconds(-1);
        }
        else
        {
            Timer.Stop();
        }
    }

    private void trailer_play(object sender, RoutedEventArgs e)
    {
        trailer.Play();
    }

    private void trailer_stop(object sender, RoutedEventArgs e)
    {
        trailer.Stop();
        trailer.Close();
    }

    private void trailer_pause(object sender, RoutedEventArgs e)
    {
        trailer.Pause();

    }
    private void ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        trailer.Volume = e.NewValue;
    }


}

}

这是 TextBlock :

<TextBlock x:Name="countDown" HorizontalAlignment="Left" Margin="40,100,0,0" TextWrapping="Wrap"  VerticalAlignment="Top"
               Grid.ColumnSpan="2" Width="250" Height="58" Foreground="White"
               Grid.Column="3" Grid.Row="1"
               Text=""/>

假设您需要一个等于 1 秒的间隔。

    private readonly DispatcherTimer Timer = new DispatcherTimer();
    private readonly DateTime daysLeft;

    public MainWindow()
    {
        InitializeComponent();

        daysLeft = DateTime.Parse("1/02/2022 12:00:00 AM");
        
        Timer.Interval = TimeSpan.FromSeconds(1.0); ;
        Timer.Tick += Timer_Tick;
        Timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        TimeSpan t = daysLeft.Subtract(DateTime.Now);
        if (t.Seconds >= 0)
        {
            string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds until launch.",
                t.Days, t.Hours, t.Minutes, t.Seconds);

            textBlock.Text = countDown;
            daysLeft.AddSeconds(-1);
        }
        else
        {
            Timer.Stop();
        }
    }