Do A on tabbedpage children button click, do B when the same button is pressed for x seconds

131 views Asked by At

I have spent my recent hours on researching on how to make this possible

I have a bottom tabbed page with a home button. What I want to achieve is, when button is pressed for x seconds, a new page shall be opened. How should the custom renderer for the tabbed page look like?

I already have one custom renderer for the tabbed page that makes sure that the home page button is a floating action button. The custom renderer for the home page button:

[assembly: ExportRenderer(typeof(NavigationBar), typeof(CustomTabbedRenderer))]
namespace MysteryLocation.Droid
{
public class CustomTabbedRenderer : TabbedPageRenderer
  {
    Context context;
    public CustomTabbedRenderer(Context context) : base(context)
    {
        this.context = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement == null && e.NewElement != null)
        {
            for (int i = 0; i <= this.ViewGroup.ChildCount - 1; i++)
            {
                var childView = this.ViewGroup.GetChildAt(i);
                if (childView is ViewGroup viewGroup)
                {
                    ((ViewGroup)childView).SetClipChildren(false);
                    for (int j = 0; j <= viewGroup.ChildCount - 1; j++)
                    {
                        var childRelativeLayoutView = viewGroup.GetChildAt(j);
                        if (childRelativeLayoutView is BottomNavigationView bottomView)
                        {
                            FloatingActionButton button = new FloatingActionButton(context);
                            
                            BottomNavigationView.LayoutParams parameters = new BottomNavigationView.LayoutParams(150, 150);
                            parameters.Gravity = GravityFlags.CenterHorizontal;
       
                            Drawable d = Resources.GetDrawable(Resource.Drawable.image);
                            button.SetScaleType(Android.Widget.ImageView.ScaleType.Center);
                            
                            button.SetImageDrawable(d);
                            button.LayoutParameters = parameters;
                            bottomView.AddView(button);
                        }
                    }
                }
            }
        }
    }
  }
}

==========

EDIT

==========

In my customtabbedrenderer I've added button.LongClick += Button_LongClick and button.Click += Button_Click with the methods

private void Button_LongClick(object sender, EventArgs e)
    {
            //open a page through pushasync
    }
private void Button_Click(object sender, EventArgs e)
    {
            //navigate to the page as usual, as it does when there's no 
              "button.Click" specified in the custom renderer
    }

Any ideas on how the code should look like in the given methods?

=========

EDIT 2

=========

I have solved on long click, a new page is being opened through

button.LongClick += (object sender, LongClickEventArgs args) =>
{
  Application.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(new Pass())); 
};

The app works both on click and on long click now. The remaining problem is, how can I allow the same button to navigate to the page that it is supposed to when only doing a simple click? It navigates to that page when there's no "button.Click += Button_Click" and "button.LongClick += Button_Click1". But as soon as I add a longclick event, the button stops working on click.

==========================

SOLVED

==========================

Added Element.CurrentPage = Element.Children[2] in the method Button_Click method, simple as that.

1

There are 1 answers

1
Mr_Engineer On

Longpress may be problematic in XF. For one of my applications I used a double tap:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
            xmlns:CustomCtrls ="clr-namespace:D4503.CustomControls"
            xmlns:Views="clr-namespace:D4503.Views"
            x:Class="D4503.Pages.MainPage"
            BackgroundColor="{StaticResource ScreenBackColor}"
            ios:Page.UseSafeArea="true">
    <ContentPage.Content>
        <StackLayout>
            <Image Grid.Row="1" Grid.Column="0" x:Name="btnRed"  Style="{StaticResource ColorButtonImage}" Source="stoplight_red.png"
                                        Aspect="AspectFit">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="Red_Button_Tapped"/>
                    <TapGestureRecognizer Tapped="Red_Button_Tapped_Double" NumberOfTapsRequired="2" />
                </Image.GestureRecognizers>
            </Image>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>