Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

TabbedPage on Android only shows the last page

$
0
0

I hope somebody can help. This has been plaguing me for a few days, and I'm at my wits end with this bug.

The problem that occurs is that I have a TabbedPage with two or more pages. In every case, the only page that my Android device will ever show is the last page. I can tap on the tabs, and the tabs on top will switch, however, the body that is shown is always just the last page. This appears to happen in a specific case where I have this navigation:

My app's root page is a MasterDetailPage ->
where the DetailPage is created outside of it's constructor and
the DetailPage contains a ListView and

when the user taps on the ListView to select an item to navigate to, I use NavigationPage.PushAsync to show the TabbedPage
in this case, the TabbedPage only shows the last page. (the yellow page)

Sorry if that is confusing, but I have full sample code to reproduce below.

This did not used to happen in Xamarin Forms v1.4. I've confirmed that it is happening on Xamarin Forms 1.5 and 2.0. Also, it only happens on Android. It works fine on iOS. I really need help here because I don't want to restructure all my code.

I'm pretty sure this is a Forms bug, unless there is something I'm obviously doing wrong.

Please help!


using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace XTabs
{
    public class RootPage : MasterDetailPage
    {
        public RootPage()
        {
            this.Master = new MenuPage(this);
            this.Detail = new Page { BackgroundColor = Color.White };
        }

        public void NavigateTo()
        {
            this.Detail = new NavigationPage(new ListPage());
        }
    }

    public class MenuPage : ContentPage
    {
        private RootPage RootPage { get; set; }

        public MenuPage(RootPage root)
        {
            this.RootPage = root;

            this.Title = "Menu";
            this.BackgroundColor = Color.Gray;

            var button = new Button { Text = "Show List" };
            button.Clicked += Button_Clicked;

            var layout = new StackLayout();
            layout.Children.Add(button);

            this.Content = layout;
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            this.RootPage.NavigateTo();    
        }
    }

    public class ListPage : ContentPage
    {
        public ListPage()
        {
            var claims = new List<string> { "First Claim", "Second Claim" };

            var list = new ListView
            {
                ItemsSource = claims,
                ItemTemplate = new DataTemplate(typeof(ListItemTemplate))
            };

            list.ItemTapped += (sender, e) => {
                this.Navigation.PushAsync(new ClaimPage(e.Item as string));
                ((ListView)sender).SelectedItem = null;
            };

            this.Content = list;
        }

        public class ListItemTemplate : ViewCell
        {
            protected override void OnBindingContextChanged()
            {
                this.View = new Label { Text = (string)this.BindingContext };
                base.OnBindingContextChanged();
            }
        }
    }

    public class ClaimPage : TabbedPage
    {
        public ClaimPage(string title)
        {
            this.Title = title;

            this.Children.Add(new DummyPage("Page One", Color.Blue));
            this.Children.Add(new DummyPage("Page Two", Color.Yellow));
        }
    }

    public class DummyPage : ContentPage
    {
        public DummyPage(string title, Color color)
        {
            this.Title = title;
            this.Content = new StackLayout { BackgroundColor = color };
        }
    }
}

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>