ASP.NET Hosting

How to Detect Long Press in .NET MAUI App using TouchBehavior?

This article will walk you through the process of utilizing TouchBehavior from the MAUI Community Toolkit to develop a long press gesture in.NET MAUI. Discover how to use parameters to initiate long press events or instructions, and view a real-time example of how to update a button picture upon a successful long press.

Establishing the Project:
To create a new project, launch Visual Studio 2022 and select Create a new project from the Start box.

Click the Next button after choosing the.NET MAUI App template and MAUI from the All project types drop-down menu in the Create a new project window:

In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:

In the Additional information window, click the Create button:


Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Install Plugin

We need to install the “CommunityToolkit.MAUI” by searching in nuget manager and click “Install” to install the plugin.

Implementation
Here, we will add the long press behaviour to the image control and you can use as per your need:

Add the below code in the xaml file to have wire-up the event or command between the designer and view model.

Add Namespace

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

Add the following code in your element. In this example, I used Image control

<Image.Behaviors>
      <toolkit:TouchBehavior LongPressCommand="LongPressCommand" LongPressDuration="2000" />
</Image.Behaviors>

The event code will be look like the below

LongPressCommand = new Command(() =>
{
	count++;

	if (count == 1)
		CounterBtn.Text = $"Long pressed {count} time";
	else
		CounterBtn.Text = $"Long pressed {count} times";

	SemanticScreenReader.Announce(CounterBtn.Text);
});

ASP.NET Core 9 Hosting Recommendation

HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.