C# ObservableCollection не добавляет элементы программно - PullRequest
0 голосов
/ 23 февраля 2020

У меня есть method, возвращающее List, чтобы установить ListView 's ItemSource. Вот точные вызовы вложенных списков.

Вот вызов TabbedPage

public ItemsMainTabbedPage(ObservableCollection<Titles> sortedItems)
{
    InitializeComponent();

    for (int i = 0; i < 3; i++)
    {
        ItemsPagePro page = new ItemsPagePro(sortedItems, i);
        this.Children.Add(page);
    }                         
}

Вот ItemsPagePro, где ListView установлено

public ItemsPagePro(ObservableCollection<Titles> sortedItems, int switcher)
{
    InitializeComponent();
    switch (switcher)
    {
        case 0:
            Title = "Home";
            listView.ItemsSource = getTabbedListResults(sortedItems, (int)TabLoadMode.All);//0
            break;
        case 1:
            Title = "Safe";
            listView.ItemsSource = getTabbedListResults(sortedItems, (int)TabLoadMode.Safe);//1
            break;
        case 2:
            Title = "Risky";
            listView.ItemsSource = getTabbedListResults(sortedItems, (int)TabLoadMode.Risky);//2
            break;
    }
}

private ObservableCollection<Titles> getTabbedListResults(ObservableCollection<Titles> sortedItems, int switcher)
{
    var itemsSource = sortedItems;
    ObservableCollection<Titles> sortedItemsMain = new ObservableCollection<Titles>();
    ObservableCollection<Item> items = new ObservableCollection<Item>();
    try
    {
        bool safe = false;
        if(switcher == 1)
        {
            safe = true;
        }
        for (int i = 0; i < itemsSource.Count; i++)
        {
            for (int j = 0; j < itemsSource[i].Count; j++)
            {
                if(itemsSource[i][j].multiPassedMatchesHTML != null)
                {
                    if (!string.IsNullOrEmpty(itemsSource[i][j].multiPassedMatchesHTML.HTMLToParse))
                    {
                        itemsSource[i][j].DetailedTips = SampleDataSource.getDetailedTips(itemsSource[i][j].multiPassedMatchesHTML.multiPassedMatches, safe);
                        if (itemsSource[i][j].DetailedTips != null)
                        {
                            if (itemsSource[i][j].DetailedTips.totalMatches > 3)
                            {
                                bool checkResults = checkBeforeResults(itemsSource[i][j]);
                                if (checkResults)
                                {
                                    itemsSource[i][j].Tip = SampleDataSource.getHighestResultFromMultiPassedMatches(itemsSource[i][j].DetailedTips);
                                    itemsSource[i][j] = switchItemTip(itemsSource[i][j]);
                                    //if (switcher != 0)
                                    //{
                                        items.Add(itemsSource[i][j]);
                                        Console.WriteLine("Item added!");
                                    //}
                                }
                            }
                        }
                    }
                }
            }
            //if(items.ToList().Count > 0)
            //{
                sortedItemsMain.Add(new Titles(items.ToList()) { Intro = itemsSource[i].Intro,  imagePath = itemsSource[i].imagePath, Summary = " - " });
            //}
            //items.Clear();
        }
        if(switcher == 0)
        {
            return itemsSource;
        }
        else
        {
            return sortedItemsMain;
        }
    }
    catch(Exception ex)
    {
        DisplayAlert("Error:", ex.Message, "OK");
        return null;
    }
}

Это сводит меня с ума. Пока switcher изменяется на 0, 1, 2, эта часть не выполняется if (switcher != 0). Я вижу, что switcher меняется с возврата метода на sortedItemsMain. Однако Items пусто при добавлении сюда items.Add(itemsSource[i][j]);. Даже если я уберу флажок для принудительного добавления элементов, список все еще будет пустым.

РЕДАКТИРОВАТЬ: Как я выяснил, эта строка испортила его. itemsSource [i] [j] .Tip = ... Когда я пытаюсь изменить его значение (строку), без каких-либо ошибок он портит остальные

...