Hello @Tony Pitman !
Here is the example code similar to handle event for multiple long-press button:
using CommunityToolkit.Maui.Behaviors;
using CommunityToolkit.Maui.Core;
using System.Threading;
using Microsoft.Maui.Dispatching;
public partial class MainPage : ContentPage
{
private Timer? _timerBtn1;
private Timer? _timerBtn2;
private int _counter1 = 0;
private int _counter2 = 0;
private readonly int _initialDelayMs = 100; // Delay before first repeat
private readonly int _repeatIntervalMs = 200; // Interval for subsequent repeats
public MainPage()
{
InitializeComponent();
// Attach handlers for each button’s TouchBehavior
btn1TouchBehavior.CurrentTouchStateChanged += (s, e) => HandleTouch(e, 1);
btn2TouchBehavior.CurrentTouchStateChanged += (s, e) => HandleTouch(e, 2);
}
private void HandleTouch(TouchStateChangedEventArgs e, int buttonId)
{
switch (e.State)
{
case TouchState.Pressed:
if (buttonId == 1)
{
_timerBtn1?.Dispose();
_timerBtn1 = new Timer(_ => OnRepeatTick(1), null, _initialDelayMs, _repeatIntervalMs);
}
else
{
_timerBtn2?.Dispose();
_timerBtn2 = new Timer(_ => OnRepeatTick(2), null, _initialDelayMs, _repeatIntervalMs);
}
break;
case TouchState.Default:
if (buttonId == 1)
{
_timerBtn1?.Dispose();
_timerBtn1 = null;
}
else
{
_timerBtn2?.Dispose();
_timerBtn2 = null;
}
break;
}
}
private void OnRepeatTick(int buttonId)
{
if (buttonId == 1)
{
_counter1++;
MainThread.BeginInvokeOnMainThread(() =>
CounterLabel1.Text = $"Button 1: {_counter1}");
}
else
{
_counter2++;
MainThread.BeginInvokeOnMainThread(() =>
CounterLabel2.Text = $"Button 2: {_counter2}");
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
_timerBtn1?.Dispose();
_timerBtn2?.Dispose();
}
}
I hope this helps! If you agree with my suggestions, please mark this as final answer, thank you!