2016-08-23 62 views
0

我有這樣的代碼只有一個孩子,當我在運行時添加意見電網出現

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) { } 
    } 
} 

我有4個類別menu.categories但是當我運行的代碼,並調用添加到網格await LoadMenu()只有一類不4, 當我調試的代碼我有4個類別列出類別,只有一個在grid.Children孩子,請任何一個有解決方案或者是錯誤

回答

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; 
     } 
相關問題