I have a renderer button in iOS and what I am trying to do is to trigger another page into the stack when it gets a swipe gesture.
if I implement that on my MainPage, for the Clicked it is quiete straight forward. as I can use "this"
public class MainPage : ContentPage
{
public MainPage ()
{
// Button bottom 1
var button = new Button {
Text = "button",
HeightRequest = 60,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
};
button.Clicked += async (sender, e) => {
await this.Navigation.PushModalAsync(new nextPage());
};
}
}
but How could I do this in a rendered button in iOS.
my renderer button is like this:
public class MYButtonRenderer : ButtonRenderer
{
UISwipeGestureRecognizer swipeGestureRecognizerUp;
protected override void OnElementChanged (ElementChangedEventArgs<Button> e)
{
base.OnElementChanged (e);
swipeGestureRecognizerUp = new UISwipeGestureRecognizer (() => onSwipeUp());
swipeGestureRecognizerUp.Direction = UISwipeGestureRecognizerDirection.Up;
if (e.NewElement == null)
{
if (swipeGestureRecognizerUp != null)
{
this.RemoveGestureRecognizer (swipeGestureRecognizerUp);
}
}
if (e.OldElement == null)
{
this.AddGestureRecognizer (swipeGestureRecognizerUp);
}
}
private void onSwipeUp()
{
//here it's where I would like to change the page to a new one.
}
}
is this possible?? Thank you for your time.
A nice way to do this would be to combine your custom renderer with a custom button view. Your custom view could have a swipe event to which you can subscribe. Of course, you could create a custom delegate if necessary as well, to pass custom event data, but I've kept this example simple.
public class CustomButton : Button
{
public event EventHandler OnSwipeUp;
public void FireSwipeUp()
{
var swipeUp = OnSwipeUp;
if (swipeUp != null)
swipeUp(this, EventArgs.Empty);
}
}
From your custom renderer, you can fire the custom event by calling the FireSwipeUp
method.
private void onSwipeUp()
{
((CustomButton)Element).FireSwipeUp();
}
Now you can subscribe to your custom OnSwipeUp
event from within your MainPage
class, just as you do with Clicked
.
// Button bottom 1
var button = new CustomButton {
Text = "button",
HeightRequest = 60,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
};
button.Clicked += async (sender, e) => {
await this.Navigation.PushModalAsync(new nextPage());
};
button.OnSwipeUp += async (sender, e) => {
await this.Navigation.PushModalAsync(new nextPage());
};