In this MAUI.NET app below the timer doesn't stop on timer.stop();
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ButtonTimer.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Button
x:Name="CounterBtn"
Text="Start"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
using System.Diagnostics;
namespace ButtonTimer;
public partial class MainPage : ContentPage
{
int count = 0;
IDispatcherTimer timer;
public MainPage()
{
InitializeComponent();
}
private void OnCounterClicked(object sender, EventArgs e)
{
timer = Application.Current.Dispatcher.CreateTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += (s, e) => Go();
if (CounterBtn.Text == "Start")
{
timer.Start();
CounterBtn.Text = "Stop";
}
else
{
timer.Stop();
CounterBtn.Text = "Start";
}
}
public void Go()
{
Debug.WriteLine("Ping");
}
}