【问题标题】:Making a timer in WPF在 WPF 中创建一个计时器
【发布时间】:2011-07-11 01:41:21
【问题描述】:

我正在为英雄联盟制作一个丛林计时器程序,当我单击一个按钮时,我将启动一个计时器,以记录丛林营地重生所需的时间。

我的问题是,我该如何制作这样的计时器?

【问题讨论】:

  • 你搜索了什么?到目前为止你有没有尝试过?

标签: wpf vb.net


【解决方案1】:

你可以使用DispatcherTimer,像这样

    ' Create a new DispatcherTimer
    Private _mytimer As New DispatcherTimer 

    Sub New()
        InitializeComponent()

        ' Set interval for timer
        _mytimer.Interval = TimeSpan.FromMilliseconds(10000)
        ' Handle tick event
        AddHandler _mytimer.Tick, ...

    End Sub

    Private Sub Button_Timer_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        ' Start timer on button click
        _mytimer.Start()
    End Sub

【讨论】:

    【解决方案2】:

    我会使用Timer 或者DispatcherTimer。为时间跨度条目创建一个文本框或其他任何内容。 .NET 的TimeSpan 类有一个构造函数或静态辅助方法,可以解析hh:mm:ss.ss 形式的字符串。按下按钮时,使用该时间跨度创建计时器(如果需要,将其转换为毫秒),并为计时器提供委托/回调方法。当时间跨度过去时,计时器将调用该方法。在该方法中添加一些代码以提醒您。

    【讨论】:

      【解决方案3】:

      来自here的回答。

      您必须使用DispatcherTime 类。为此,请在文档的开头导入以下类。

      Imports System.Windows.Threading 
      

      然后在Class MainWindow之后,用标识符(tmr1)声明DispatchTimer:

      Dim tmr1 As New DispatcherTimer 
      

      然后在声明后加上这段代码:

      Sub New()
      InitializeComponent()
      tmr1 .Interval = Timespan.FromSeconds(6) 
      AddHandler tmr1.Tick, AddressOf tmr1Tick
      tmr1 .Start() 
      End Sub 
       
      Private Sub tmr1Tick(sender As System.Object, e As System.Windows.RoutedEventArgs)
      'If errors exist, then type the "Private Sub tmr1Tick...End Sub"
      'Enter your code. This code is for when the timer ticks
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-22
        • 2021-04-09
        • 2011-02-06
        相关资源
        最近更新 更多