首页 > 解决方案 > 我可以在标签页中使用 ClassId 来区分它们的内容吗

问题描述

我正在尝试将同一页面用于 TabbedPage 中的 3 个选项卡。但是每个选项卡必须在列表视图中显示不同的数据。有没有办法为每个选项卡设置一个参数?

例子

    <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"/> 

我尝试使用 ClassId 和 Title 来获得它们的区别,但是在 Sales 类构造函数中检索 ClassId 时遇到问题,还有其他方法可以获得我想要的输出吗?

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

标签: xamarin.formstabbedpage

解决方案


您可以在OnAppearing方法中加载数据:

protected override void OnAppearing()
{
    base.OnAppearing();

    Console.WriteLine(ClassId + "OnAppearing");

    BindingContext = this;
    salesCollection = new ObservableCollection<Head>();
    initLvw();

}

delay或者在构造函数中加载一点数据:

public AboutPage()
{
    InitializeComponent();

    Task.Run(async () =>
    {
        await Task.Delay(200);
        Console.WriteLine(ClassId + "AboutPage");

        BindingContext = this;
        salesCollection = new ObservableCollection<Head>();
        initLvw();
    });
}

推荐阅读