Start an eventhandler when my timer value reaches 0

This is my code for a timer, I want to load my gameover.xaml once my timer reaches 0. How do I connect the event.

    public void StartTimer(object o, RoutedEventArgs sender)
    {
        myDispatcherTimer = new DispatcherTimer();
        myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 1000 Milliseconds 
        myDispatcherTimer.Tick += new EventHandler(Each_Tick);
        myDispatcherTimer.Start();
    }

    // counter
    int i = 60;


    public void Each_Tick(object o, EventArgs sender)
    {
       timerBlock.Text = "Time: " + i--.ToString() + "s";
    }

And here is my event method

    private void gameOverEvent(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/gameOver.xaml", UriKind.Relative));
    }

how can I do this -

if (i <= 0)
    something += new EventHandler(gameOverEvent);

What shall I put in something.

Answers


you dont need to have a gameOverEvent at all, you could just check if i = 0 in the Each_Tick event handler, and navigate to the game over page if it is

public void Each_Tick(object o, EventArgs sender) 
{ 
   timerBlock.Text = "Time: " + i--.ToString() + "s";

   if(i = 0)
   {
         NavigationService.Navigate(new Uri("/gameOver.xaml", UriKind.Relative));
   }
}

if (i <= 0)
    gameOverEvent(null, null);

You have to implement an Eventhandler in your class like

public event EventHandler GameLost;

From this class you can raise the event

if ((this.GameLost != null) && (i <= 0))
    this.GameLost(this, new EventArgs());

See this tutorial for a more detailed guide.


Need Your Help

Getting a web-font to work on an HTML5 Windows Phone App?

javascript windows-phone-8 fonts

I'm building a Windows Phone 8 HTML5 app that shows an external website on the web. When the page loads, it shows all the content along with the correct css styling except for the web font. The web...

How to detect the pressed button and display it on Another View? Objective-C

ios objective-c xcode4.5 history calculator

I am new to iOS app development. I want to create a Calculator App in iOS that has split view. The left side is the "History" Feature in Scroll View and the right side is the calculator itself. Now,