Search code examples
wpfvb.nettimermobile-applicationdispatchertimer

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


I have a timer. in my form is still possible to open the child window. when I open and close this window, the timer starts again. How can I continue timer operation when I open and clos the child window??? I hope very much for your help! this is my timer:

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

Best regards, Polina


Solution

  • Frame.Navigate(GetType(Location)) will initialize new instance of Location page, so CountUp value will be lost.

    You can make CountUp field public and add another field in parent object, for example, SavedCountUpValue. Then use Location.Unloaded event to save CountUp value into SavedCountUpValue field.

    In parent object, in Location_Unloaded handler:

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

    Then, when initializing new Location object, restore CountUp value.

    In parent object, in btnMapPoint_Click handler:

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