1

How should I resolve the issue?

The suggested alternatives do not work either.

Error CS1545 Property, indexer, or event 'DispatcherTimer.Tick' is not supported by the language; try directly calling accessor methods 'DispatcherTimer.add_Tick(EventHandler)' or 'DispatcherTimer.remove_Tick(EventRegistrationToken)'

_timer = new DispatcherTimer();
_timer.Tick += Timer_Tick;
_timer.Interval = new TimeSpan(0, 0, 1);
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
curiousMind_85
  • 167
  • 1
  • 8
  • We can't see Timer_Tick, it is probably not a proper method. – Hans Passant Dec 22 '21 at 14:15
  • I think it is for WPF – Bas H Dec 22 '21 at 14:18
  • I don't think Xamarin Forms supports WPF's DispatcherTimer control. Here is how to create a timer in Xamarin Forms: https://stackoverflow.com/q/37203371/87698 – Heinzi Dec 22 '21 at 14:31
  • Unfortunately, the suggested Device.StartTimer class is not recognized by IntelliSense. – curiousMind_85 Dec 22 '21 at 18:41
  • use System.Timers – Jason Dec 22 '21 at 20:46
  • 1
    Per this [quote](https://stackoverflow.com/a/58447523/199364), the built-in .net Timer in System.Timers works well. But I'll also comment that Device.StartTimer is in `namespace Xamarin.Forms`. After you typed `Device.`, if you wait, Intellisense should put red underline, and give you lightbulb (or maybe a drop-down) with choices `using Xamarin.Forms;` or to fully specify as `Xamarin.Forms.Device`. – ToolmakerSteve Dec 22 '21 at 22:43

1 Answers1

3

Windows.UI.Xaml DispatcherTimer is only apply to WinRT, and System.Windows.Threading DispatcherTimer is only apply to .NET Framework. So we can't use both of them into forms. For using timer in Xamarin forms please use Device.StartTimer to replace.

Device.StartTimer (new TimeSpan (0, 0, 60), () =>
{
    // do something every 60 seconds
    Device.BeginInvokeOnMainThread (() => 
    {
      // interact with UI elements
    });
    return true; // runs again, or false to stop
});
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Okay, but I'd like to interact with the UI every 600 miliseconds, not every 60 seconds. And it does not offer me the opportunity to write 0.6 seconds directly as value of third parameter. – curiousMind_85 Dec 23 '21 at 08:43
  • `new TimeSpan(0, 0, 0, 600)` or `TimeSpan.FromMilliseconds(600)` use this to replace above timespan. – Nico Zhu Dec 23 '21 at 08:46
  • 1
    I've just inserted the Device.StartTimer code snippet in the App() constructor located inside App.xaml.cs .I've also created a private bool variable _shouldTimerRun inside App class and I've replaced return true; with return _shouldTimerRun; There's an event available to start or stop the timer by setting the bool variable value. Is there anything else I should do in order to make the timer fully functionable? – curiousMind_85 Dec 23 '21 at 09:08
  • use private bool variable _shouldTimerRun to stop timer is recommend way. StartTimer has not provided other entry to manage timer. – Nico Zhu Dec 23 '21 at 09:30
  • 1
    I've just marked the answer as accepted. Thank you very much for your feedback! :) – curiousMind_85 Dec 24 '21 at 14:13