does anybody knows if there is a workaround for using long touch with DotNet MAUI? In my legacy app build with Xamarin, i was using xct:TouchEffect.LongPressCommand.
As far as i know there is a wip in MAUI community toolkit but it's not avalaible at this time.
Maybe using EventToCommandBehaviour? What will be the name of the event if i use this?
Is there a way to extend for exemple ImageHandler with propertyMapper and CommandMapper to achieve this goal? Maybe someone has an other idea? Thanks by advance for any suggestion π
It would be fantastic to have the long touch effect with .Net MAUI and there is a proposal on Github to include this in the MAUI Community Toolkit.According to the thread update, it'll be implemented soon.
As an alternative workaround, you can try to find the handler of the MAUI control, then acquire handler.PlatformView
which is a native control. After that, you can invoke native control methods, and subscribe to native control events such as the gesture event, see Customize .NET MAUI controls with handlers and refer to the following code:
Customize a control with a mapper(be effective on an Image control):
Microsoft.Maui.Handlers.ImageHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
{
#if WINDOWS
handler.PlatformView.Holding += PlatformView_Holding;
#endif
#if ANDROID
handler.PlatformView.LongClick += PlatformView_LongClick;
#endif
#if IOS
handler.PlatformView.UserInteractionEnabled = true;
handler.PlatformView.AddGestureRecognizer(new UILongPressGestureRecognizer(HandleLongClick));
#endif
});
Implement Long press event in each platform:
#if WINDOWS
private void PlatformView_Holding(object sender, Microsoft.UI.Xaml.Input.HoldingRoutedEventArgs e)
{
//Touch can produce a Holding action, but mouse devices generally can't.
//see https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.holding?view=winrt-22621
}
#endif
#if IOS
private void HandleLongClick(UILongPressGestureRecognizer sender)
{
//raise longpress event
}
#endif
#if ANDROID
private void PlatformView_LongClick(object sender, Android.Views.View.LongClickEventArgs e)
{
//raise longpress event
}
#endif
Update:
public class TestPageViewModel
{
public TestPageViewModel() {
ModifyImage();
}
void ModifyImage()
{
Microsoft.Maui.Handlers.ImageHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
{
#if WINDOWS
ββββββββββββββββββhandler.PlatformView.Holding += PlatformView_Holding;
#endif
#if ANDROID
handler.PlatformView.LongClick += PlatformView_LongClick;
#endif
#if IOS
ββββββββββββββββββhandler.PlatformView.UserInteractionEnabled = true;
ββββββββββββββββββhandler.PlatformView.AddGestureRecognizer(new UILongPressGestureRecognizer(HandleLongClick));
#endif
});
}
#if WINDOWS
private void PlatformView_Holding(object sender, Microsoft.UI.Xaml.Input.HoldingRoutedEventArgs e)
ββββββ{
//Touch can produce a Holding action, but mouse devices generally can't.
//see https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.holding?view=winrt-22621
}
#endif
#if IOS
private void HandleLongClick(UILongPressGestureRecognizer sender)
{
//raise longpress event and then call the command here
}
#endif
#if ANDROID
private void PlatformView_LongClick(object sender, Android.Views.View.LongClickEventArgs e)
{
//raise longpress event and then call the command here
}
#endif
}
}
Reference link: https://learn.microsoft.com/en-us/answers/questions/900859/long-press-in-net-maui