How i can handle short and long button clicks? I need some specific action on short button click and specific action on long button click. I read about Gesture Listener and try to implement it into Android MainActivity.cs file(MainActivity class). But i have exception when application runs.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test.Page">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<ListView x:Name="MyButton" Grid.Row="0" />
<ScrollView Orientation="Horizontal" Grid.Row="1">
<Label x:Name="MyLabel" HorizontalOptions="Center">...</Label>
</ScrollView>
</Grid>
</ContentPage>
To do this you could use a custom renderer for the button. Each platform would have its own way of handling a long gesture which is not currently exposed through the forms button.
On Android:
[assembly: Xamarin.Forms.ExportRenderer (typeof (MyButton), typeof (MyButtonRenderer))]
namespace MyApp.Android
{
public class MyButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<global::Xamarin.Forms.Button> e)
{
base.OnElementChanged (e);
if (e.OldElement == null) {
var nativeButton = Control;
nativeButton.LongClick += delegate {
//Do something
};
}
}
}
}
On iOS:
[assembly:ExportRenderer (typeof(ButtonWithLongPressGesture), typeof(LongPressGestureRecognizerButtonRenderer))]
namespace SampleApp.iOS
{
public class LongPressGestureRecognizerButtonRenderer : ButtonRenderer
{
ButtonWithLongPressGesture view;
public ButtonPressGestureRecognizerImageRenderer ()
{
this.AddGestureRecognizer (new UILongPressGestureRecognizer ((longPress) => {
if (longPress.State == UIGestureRecognizerState.Began) {
view.HandleLongPress(view, new EventArgs());
}
}));
}
protected override void OnElementChanged (ElementChangedEventArgs<Button> e)
{
base.OnElementChanged (e);
if (e.NewElement != null)
view = e.NewElement as ButtonWithLongPressGesture;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.