Search code examples
buttonxamarintabbedpage

Xamarin.Forms: Button in TabbedPage


How to add button to other page on this code (this is part of one card in TabbedPage):

this.Children.Add(new ContentPage
        {
            Title = "Text",
            Content = new StackLayout
            {
                Padding = 20,
                VerticalOptions = LayoutOptions.FillAndExpand,
                Children =
                {
                    new Image
                    {
                        Source = ImageSource.FromFile("image.png")
                    },
                    new Label
                    {
                        TextColor = Color.FromHex("#5F5A5A"),
                        FontSize = 16,
                        Text = "Other text"  
                    }
             }
            }
        });

Thanks for help.


Solution

  • If I understood your issue right, you just need button in another tab?

    this.Children.Add(new ContentPage
            {
                Title = "Text",
                Content = new StackLayout
                {
                    Padding = 20,
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    Children =
                    {
                        new Image
                        {
                            Source = ImageSource.FromFile("image.png")
                        },
                        new Label
                        {
                            TextColor = Color.FromHex("#5F5A5A"),
                            FontSize = 16,
                            Text = "Other text"  
                        }
                 }
                }
            });
    this.Children.Add(new ContentPage
            {
                Title = "Button",
                Content = new StackLayout
                {
    Children ={
                  new Button{
    Image = ImageSource.FromFile("button.png"),
    Backgroundcolor = "Transparent"
    
    }
                 }
                }
            });
    

    Alternatively you can use tap gesture recognizer

    var button = new Image {  }; 
    button.GestureRecognizers.Add (new TapGestureRecognizer (sender => {
    
    }));