Element Triple tap - xamarin

How can i perform triple tap in Xamarin on a specific element?
I tried with for loop performing a single tap 3 times, but it looks like it is slow cause it's not working.
public void AppLaunches()
{
for(int i = 0; i <= 3;i++)
{
app.Tap(x => x.Css(".app-logo"));
}

You can add TapGestureRecognizer in code or XAML and set the number of taps required to trigger the associated event or command:
XAML using event handler:
<Label Text="Welcome to Xamarin Forms!" VerticalOptions="Center" HorizontalOptions="Center" >
<Label.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="3" Tapped="Handle_Tapped"/>
</Label.GestureRecognizers>
</Label>
The Event handler:
void Handle_Tapped(object sender, System.EventArgs e)
{
System.Diagnostics.Debug.WriteLine($"Triple tap on {sender}");
}
Re: Adding a Tap Gesture Gesture Recognizer
Note: Triple taps work fine on iOS, but Android does not support triple tabs as a standard feature (it does support double taps).
Re: https://bugzilla.xamarin.com/show_bug.cgi?id=32874
If you need triple tabs on Android, you can capture each tap individually (NumberOfTapsRequired="1"). If it is the first tap, set a local variable to 1 and start a background timer that expires in X milliseconds, if the next tab occurs before the timer has expired, restart the timer and 1 to your local variable, repeat until you get three taps. If the timer expires have it reset the local variable back to 0.

Related

.net maui CarouselView is not quite compatible with Slider control: CarouselView swipe operation takes over Slider's drag action

.net maui app.
Dragging value element along the slider bar does not work if the the slider put into CarouselView's template like this:
<CarouselView ItemsSource="{Binding Items}">
<CarouselView.ItemTemplate>
<DataTemplate>
<Slider Minimum="0" Maximum="30" WidthRequest="200" />
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
CarouselView takes over the swipe event for scrolling through the items, and Slider does not get the event (DragStarted is not even called). You can actually click along the slider bar to change its value, so it's not completely frozen, but not how it's supposed to work. Drag & drop is main way user deal with slider control.
Could anyone advise any workaround? I want users to be able scroll through carousel view items also. It's just if they swipe inside the control, event should not handed over to its parent container, if it's possible to do so.
If I add it outside of the corouselview, combine both in Grid and use padding to align slider inside the corouselview, it works as expected, but I need to add lots of additional code, calculate the desirable location and redirect all bindings, which ends up to be an awkward workaround.
At first, I don't suggest you use the slider in the CarouselView. Becasue you want the same behavior has two effects. There is a conflict between them.
But for the android, you can use the custom handler to deal with the swipe event.
Put the Handler class in the /Platform/Android:
public class MySliderHander : SliderHandler
{
protected override void ConnectHandler(SeekBar platformView)
{
base.ConnectHandler(platformView);
platformView.SetOnTouchListener(new SliderListener());
// the listener will make the slider deal with the swip event not the CarouselView.
}
}
Put the SliderListener class in the /Platform/Android
public class SliderListener : Java.Lang.Object, IOnTouchListener
{
public bool OnTouch(global::Android.Views.View v, MotionEvent e)
{
if (e.Action == MotionEventActions.Down || e.Action == MotionEventActions.Move)
{
v.Parent.RequestDisallowInterceptTouchEvent(true);
}
else
{
v.Parent.RequestDisallowInterceptTouchEvent(false);
}
return false;
}
}
And in the MauiProgram.cs:
builder
                  .UseMauiApp<App>()      
                  .ConfigureMauiHandlers(handlers => {
#if ANDROID
                        handlers.AddHandler(typeof(Slider), typeof(YourProjectName.Platforms.Android.MySliderHander));
                  #endif
                  })
In addition, the Slider's height is same as the CarouselView. So you can use a frame to contain the Slider and swipe the CarouselView by swiping the frame.

Stop event propagation in NativeScript ios tap event

I have a tap event in my code. Its parent(not an immediate parent) also has tap event. In Android, everything works fine. But in ios, the event is propagating upwards to that parent. How to stop that event propagation (such as event.stopPropagation() in javascript).
Sample XML code:
<StackLayout tap="newsDetails" data-args="{{ $value }}">
<StackLayout orientation="horizontal" >
<Label text="Share" class="icon" tap="shareNews" data-args="{{ $value }}"/>
</StackLayout>
</StackLayout>
Note: I am using NativeScript core
Thank you.
You can use the boolean property isUserInteractionEnabled, however, this won't solve the issue as it will stop the user interaction for all nested elements as well. It is simply not good practice to have multiple tap events in the same area.
I had the same issue on android, here's a simple solution i came up with, not elegant but works :
private ignoreTap = false;
shareNews(){
this.ignoreTap = true;
// ...
}
newsDetails(){
if (this.ignoreTap) { this.ignoreTap = false; return; }
// ...
}
The flag ignoreTap will stop the root function execution.

Does the iOS simulator correct handle the tap event with Xamarin?

I have added a GestureRecognizer like this:
faveLabel.Text = "ABC";
faveLabel.BackgroundColor = Color.Red;
faveLabel.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = new Command(() => OnLabelClicked())
});
}
private void OnLabelClicked()
{
throw new NotImplementedException();
}
Here is the XAML:
<StackLayout Grid.Row="0" Grid.Column="0" Padding="15,10,20,10" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand">
<Label x:Name="faveLabel" XAlign="Center" FontSize="23" />
</StackLayout>
Using the simulator it almost never works and when I did get it to work it seemed like I had to click above the text and not on the text. When I debug on my phone it works okay.
Are there some issues where the simulator does not respond correctly?
There are no issues that I know of. I haven't seen any weird myself unless I actually had something else causing it. You could try to give your Label a bright background color to see where it is actually positioned. Perhaps some of your other elements are overlapping it or something else in your UI is stopping the gesture recognizer. A common example is when you put one in a ListView which has a tap event of its own which might conflict with a TapGestureRecognizer.

How to automatically call Event in Windows Phone 8

How to automatically raise event in windows phone? For example, I have an element <Image name = "image" .... />. I want when a MainPage is loaded, it will automatically raise tap event on that element
If you want to declare tap event dynamically (loads tap event on page load), You can declare it in following way. Here is your xaml.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image Name="image1"/>
</Grid>
And in constructor,
public MainPage()
{
InitializeComponent();
image1.Tap += image1_Tap;
}
void image1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//Perform your action here
//This method invokes only when you tap on image
}
Else, try the other way.
Loaded += (s, e) =>
{
//Actions that are performed when image is tapped
}
Add above lines in your constructor.
This probably is not a good solution to whatever you are trying to accomplish. It can be done with reflection - How to manually invoke an event? However I'd just extract your Tap event code to method and then call this method in your Loaded event and also Tap even.

Get scroll event for ScrollViewer on Windows Phone

Question:
Get scroll event for ScrollViewer on Windows Phone
I have a scrollviewer like so:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer x:Name="MyScroller">
<StackPanel>
<!-- ... -->
</StackPanel>
</ScrollViewer>
</Grid>
I need the event for when the scrolling occurs for MyScroller:
// MyScroller.Scroll += // <-- "Scroll" event does not exist on ScrollViewer
MyScroller.MouseWheel += MyScroller_MouseWheel; // Does not fire on scroll
MyScroller.ManipulationDelta += MyScroller_ManipulationDelta; // Fires for pinch-zoom only
MouseMove fires when ScrollViewer is scrolled:
public MainPage()
{
InitializeComponent();
MyScroller.MouseMove += MyScroller_MouseMove;
}
void MyScroller_MouseMove(object sender, MouseEventArgs e)
{
throw new NotImplementedException();// This will fire
}
It isn't intuitive, since it is named as a "mouse" event and there is no mouse on the phone. The touch point does move, however, relative to the ScrollViewer container, which is how it can handle scrolling.
It's not that simple, but there's a few scroll detection mechanisms written in this question:
WP7 Auto Grow ListBox upon reaching the last item
Basically take a look at the way OnListVerticalOffsetChanged is called and used.
With Mango, you can watch for the "ScrollStates" visual state to change as described in this sample project.

Resources