当我在运行时向Grid添加视图时,只显示一个子视图

本文关键字:视图 显示 一个 运行时 Grid 添加 | 更新日期: 2023-09-27 18:03:48

我有这样的代码

public class CategoryComponent : Button
{
public CategoryComponent(CategoryModel _model) : base()
{
Text = _model.name;
}
}
public class MenuComponent : ContentView
{
    ContentView header;
    ContentView content;
    List<CategoryComponent> categories = new List<CategoryComponent>();
    public MenuComponent()
    {
        var grid = new Grid
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            RowSpacing = 10,
            ColumnDefinitions =
            {
                new ColumnDefinition {Width = new GridLength(1,GridUnitType.Star) },
            },
            RowDefinitions =
            {
                new RowDefinition { Height = new GridLength(50,GridUnitType.Absolute)},
                new RowDefinition { Height = new GridLength(1,GridUnitType.Star)},
            },
        };
        header = new ContentView
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
        };
        grid.Children.Add(new ScrollView
        {
            Orientation = ScrollOrientation.Horizontal,
            Content = header,
        }, 0, 0);
        content = new ContentView
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            Content = new ActivityIndicator { IsVisible = true, IsRunning = true },
        };
        grid.Children.Add(new ScrollView
        {
            Orientation = ScrollOrientation.Vertical,
            Content = content,
        }, 0, 1);
        Content = new Frame
        {
            Content = grid,
            OutlineColor = Color.White,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
        };
    }
    public async void LoadMenu()
    {
        try
        {
            var menu = await MenuManager.GetMenu();
                header.Content = null;
                content.Content = null;
                categories.Clear();
                int i = 0;
                var grid = new Grid
                {
                    ColumnSpacing = 5,
                    RowDefinitions =
                    {
                        new RowDefinition {Height = new GridLength(1,GridUnitType.Star) }
                    }
                };
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
    // there is 4 categories in menu.categories
                foreach (var category in menu.categories)
                {
                    i++;
                    grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(150, GridUnitType.Absolute) });
                    var catcomponent = new CategoryComponent(category);
        // adding to the list
                    categories.Add(catcomponent);
        // adding to the grid
                    grid.Children.Add(catcomponent, i, 0);
                }
                grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
                header.Content = grid;
        }
        catch (Exception exc) { }
    }
} 

我在menu.categories中有4个类别,但当我运行代码并调用await LoadMenu()时,只有一个类别添加到网格中,而不是4,当我调试代码时,我在列表类别中有4个类别,在网格中只有一个孩子。孩子们,请问谁有解决办法或者错误在哪里

当我在运行时向Grid添加视图时,只显示一个子视图

问题是:

public override bool Equals(object obj)
        {
            CategoryComponent cat = obj as CategoryComponent;
            if (cat != null)
            {
//the right is this.Text == cat.Text
                if (cat.Text == cat.Text)
                    return true;
            }
            return false;
        }