Blazor Custom Longpress -Taste - Stellen Sie sicher, dass die Aktion nur einmal auslöstC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Blazor Custom Longpress -Taste - Stellen Sie sicher, dass die Aktion nur einmal auslöst

Post by Anonymous »

Wir haben eine Blazor -Web -Benutzeroberfläche mit Mudblazor -Komponenten, die auf einem Kiosk -PC mit einem TouchScRen ausgeführt werden. Das Problem ist, dass unsere Benutzer häufig ihren Finger auf einen Knopf legen und sie dort einfach halten. Auf diese Weise schießt das Onclick -Ereignis nicht, also passiert nichts. Dann beschweren sie sich, dass unser Kiosk nicht funktioniert. /> Die Normal -Taste drückt immer noch < /li>
< /ul>
Wie ich es implementiert habe:
auf Ontouchstart Ein 300 -ms -Timer wird gestartet (oder ein Aufgabe.Delay). gestoppt < /li>
Das "_actionexecuted" -Bol wird überprüft. Wenn es falsch ist, ist es sofort auf True eingestellt und die Aktion wird ausgeführt. Wenn es bereits stimmt, geschieht nichts, ich kehre nur zurück.@inherits MudButton


@ChildContent

< /code>
private async Task ExecuteAction(bool fromTimer)
{
string origin = fromTimer ? "timer" : "touch end";
if (_actionExecuted)
{
Console.WriteLine($" KioskButton - {origin} - {DataTestId} - action tried to double-fire.");
return;
}// Prevent multiple calls
_actionExecuted = true;

Console.WriteLine($"a) KioskButton - {origin} - {DataTestId} - action triggered and passed check.");

if (OnAction != null)
{
Console.WriteLine($"b) KioskButton - {origin} - {DataTestId} - performing OnAction.");
await OnAction(Parameters);
}
}

private void StartTimer()
{
StopTimer(); // Ensure no previous timer is running
_actionExecuted = false;
_cts = new CancellationTokenSource();

Task.Delay(HoldDuration, _cts.Token).ContinueWith(async t =>
{
if (!t.IsCanceled)
{
await ExecuteAction(true);
}
}, TaskScheduler.Default);
}

private async Task HandleRelease()
{
StopTimer(); // Stop the hold timer
await ExecuteAction(false); // Execute if not already triggered
}

private void StopTimer()
{
_cts?.Cancel();
_cts?.Dispose();
_cts = null;
}
< /code>
I was under the impression this code should have prevented double-firing of the action. But it does happen, actually quite often - maybe one out of 5 clicks (although when I try to reproduce it on purpose, it stops happening). Is there a way to make absolutely sure the button never double-fires?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post