Search code examples
xamarin.formstabbedpage

Can I use ClassId in tabbedpage to differentiate their content


I'm trying to use the same page for 3 tabs in TabbedPage. But each tab has to display different data in the listview. Is there a way to set a parameter for each tab?

Example

    <local:Sales Title="Pending" 
                 Icon="ic_shortcut_home.png"
                 ClassId="pending"/>

    <local:Sales Title="Posted" 
                 Icon="ic_shortcut_home.png"
                 ClassId="posted"/>

    <local:Sales Title="Uploaded" 
                 Icon="ic_shortcut_home.png"
                 ClassId="uploaded"/> 

I tried using ClassId, and Title to get their difference but I'm having trouble retrieving the ClassId in the Sales class constructor, are there any other ways to get the output I want?

        public Sales()
        {
            InitializeComponent();
            BindingContext = this;
            salesCollection = new ObservableCollection<Head>();
            initLvw();
            Console.WriteLine(Title); //returns null
            Console.WriteLine(ClassId); // returns null
        }

Solution

  • You can load data in OnAppearing method:

    protected override void OnAppearing()
    {
        base.OnAppearing();
    
        Console.WriteLine(ClassId + "OnAppearing");
    
        BindingContext = this;
        salesCollection = new ObservableCollection<Head>();
        initLvw();
    
    }
    

    Or load data with a little delay in constructor:

    public AboutPage()
    {
        InitializeComponent();
    
        Task.Run(async () =>
        {
            await Task.Delay(200);
            Console.WriteLine(ClassId + "AboutPage");
    
            BindingContext = this;
            salesCollection = new ObservableCollection<Head>();
            initLvw();
        });
    }