打开/关闭子窗口时,如何继续计时器的时间

时间:2016-09-05 09:37:33

标签: wpf vb.net timer mobile-application dispatchertimer

我有一个计时器。在我的表单中仍然可以打开子窗口。当我打开并关闭此窗口时,计时器再次启动。当我打开和关闭子窗口时,如何继续计时器操作?我非常希望得到你的帮助! 这是我的计时器:

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

子窗口:

    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

最好的问候,Polina

1 个答案:

答案 0 :(得分:0)

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

您可以将CountUp字段设为公开,并在父对象中添加其他字段,例如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