【问题标题】:Adding a countdown timer to textblock WPF向文本块 WPF 添加倒数计时器
【发布时间】:2021-12-15 20:55:51
【问题描述】:

我想用 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 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=""/>

【问题讨论】:

    标签: c# .net wpf xaml countdown


    【解决方案1】:

    假设您需要一个等于 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();
            }
        }
    

    【讨论】:

    • 这似乎是一个很好的方法。我收到一个错误 CS0120 告诉我“非静态字段、方法或属性 'TextBlock.Text' 需要对象引用”我尝试了几件事并在谷歌上搜索了它的含义,但可能是找不到它 *** 我复制粘贴你发给我的代码 :) 再次感谢你!
    • 你在使用绑定吗?这段代码对我有用。
    • 什么是绑定? :$
    • XAML 中的数据绑定...无论如何,请创建一个新的 WPF 项目并将我的代码复制粘贴到 MainWindow.xaml.cs,然后添加一个名为“textBlock”的 TextBlock 到主窗口网格。它应该工作!
    • 非常感谢您的帮助,使我能够使用最后的评论。我没有更改我的 TextBlock 名称:P
    猜你喜欢
    • 2023-02-10
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多