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

时间:2016-08-23 14:32:43

标签: c# xamarin xamarin.forms cross-platform

我有这段代码

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.Children中只有一个孩子,请任何人有解决方案或错误在哪里

1 个答案:

答案 0 :(得分:1)

问题是:

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;
        }