打开/关闭 child window 时如何继续计时

How can I continue time of the timer when I opening / closing the child window

我有一个计时器。在我的表格中仍然可以打开childwindow。当我打开和关闭这个 window 时,计时器再次启动。打开和关闭child window时如何继续定时器操作???非常希望得到您的帮助! 这是我的计时器:

Private timer As DispatcherTimer
Private CountUp As Integer

Public Sub DispatcherTimerSetup()

    timer = New DispatcherTimer()
    timer.Interval = New TimeSpan(0, 0, 1)
    AddHandler timer.Tick, AddressOf timer_Tick
    timer.Start()

End Sub

Private Sub timer_Tick(sender As Object, e As Object)

    CountUp += 1
    Dim counter As TimeSpan
    counter = TimeSpan.FromSeconds(CountUp)
    txblCountdown.Text = counter.ToString("mm\:ss")

End Sub

child window:

    Private Sub btnMapPoint_Click(sender As Object, e As RoutedEventArgs)

    SaveControlValuesInObject()

    Dim intIndex As Integer = CInt(sender.Name.Replace("btnMapPoint_", ""))

    Frame.Navigate(GetType(Location))
    TryCast(Frame.Content, Location).InitForm_Observation(_myEventErist, intIndex, GetType(Event9900000))
    TryCast(Frame.Content, Location).IsChangeMapEnabled = False
    TryCast(Frame.Content, Location).SetSelectedMap(DirectCast(cboMesspunkt.SelectedItem, SMS_KARTE))
End Sub

此致,波琳娜

Frame.Navigate(GetType(Location)) 将初始化 Location 页面的新实例,因此 CountUp 值将丢失。

您可以创建 CountUp 字段 public 并在父对象中添加另一个字段,例如 SavedCountUpValue。然后使用 Location.Unloaded 事件将 CountUp 值保存到 SavedCountUpValue 字段中。

在父对象中,在 Location_Unloaded 处理程序中:

SavedCountUpValue = TryCast(Frame.Content, Location).CountUp

然后,在初始化新的 Location 对象时,恢复 CountUp 值。

在父对象中,在 btnMapPoint_Click 处理程序中:

Frame.Navigate(GetType(Location))
...
TryCast(Frame.Content, Location).CountUp = SavedCountUpValue