1

How can I implement a countdown timer in my Windows Phone 8.1 app? There seems to be no information available for it. All I could find works for either a Windows Forms application or a Windows Application, but none of them seems to be working on the phone app.This is what I am doing-

namespace Timer
{
    public partial class MainPage : Page
    {           
        DispatcherTimer mytimer = new DispatcherTimer();
        int currentcount = 0;
        public MainPage()
        {
            InitializeComponent();

            mytimer = new DispatcherTimer();
            mytimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
            mytimer.Tick += new EventHandler(mytime_Tick);
              //HERE error comes Cannot implicitly convert type System.EventHandler to System.EventHandler<object>
        }

        private void mytime_Tick(object sender,EventArgs e)
        {
            timedisplayBlock.Text = currentcount++.ToString();    
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            mytimer.Start();
        }
    }
}

But it gives me this error- Cannot implicitly convert type System.EventHandler to System.EventHandler<object>

3 Answers3

1

Should your increment not be written as

timedisplayBlock.Text = (++currentcount).ToString();

Or

currentcount++;
timedisplayBlock.Text = currentcount.ToString();

I don't think the increment will matter too much but still should be written correctly to ensure you do not leave yourself a count behind. - See Sergiol's issue with David's answer on the link below https://stackoverflow.com/a/7848129/2110465

The other thing i have noticed is that you are initializing the DispatcherTimer twice...

DispatcherTimer mytimer = new DispatcherTimer();
...
..
mytimer = new DispatcherTimer();

It is better practice to initialize upon the instance to save overhead, though depending on the scope of use. Given i am not aware of how the rest of your code uses it, i suggest it be rewritten as follows

namespace Timer
{
    public partial class MainPage : Page
    {
        DispatcherTimer mytimer = new DispatcherTimer();
        int currentcount = 0;

        public MainPage()
        {
            InitializeComponent();

            mytimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
            mytimer.Tick += new EventHandler(mytime_Tick);
        }

        private void mytime_Tick(object sender, EventArgs e)
        {
            timedisplayBlock.Text = (++currentcount).ToString();
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            mytimer.Start();
        }
    }
}

Saying all of the above, i could not reproduce the fault when replicated...

EDIT - This may be your issue (https://stackoverflow.com/a/16636862/2110465)

Community
  • 1
  • 1
RussDunn
  • 175
  • 1
  • 12
0

You misspelled the event handler name the event handler definition needs to be changing too, it should be:

mytimer.Tick += mytime_Tick; // removed the 'r'
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
  • Now I am getting this error-`Cannot implicitly convert type System.EventHandler to System.EventHandler` –  Feb 14 '15 at 22:11
  • `Cannot implicitly convert type System.EventHandler to System.EventHandler` –  Feb 14 '15 at 22:13
  • Yeah I already fixed that error with an extra `r`.Now it is giving me this new error which I mentioned. –  Feb 14 '15 at 22:20
  • I tried the same code with Windows phone silverlight,it is working but wirh windows phone blank app,it is giving the above error?What can I do to fix this?Also `using System.Windows.Threading ` is not working in `blank app` –  Feb 14 '15 at 22:27
0

Change
private void mytimer_Tick(object sender, EventArgs e)
to
private void mytimer_Tick(object sender, object e).

Łukasz Rejman
  • 1,892
  • 1
  • 11
  • 18