C#ObservableCollection不以编程方式添加项
我有一个C#ObservableCollection不以编程方式添加项,c#,list,asynchronous,mvvm,observablecollection,C#,List,Asynchronous,Mvvm,Observablecollection,我有一个方法返回一个列表来设置列表视图的项目源。 下面是嵌套列表的确切调用 下面是选项卡页面的调用 public ItemsMainTabbedPage(ObservableCollection<Titles> sortedItems) { InitializeComponent(); for (int i = 0; i < 3; i++) { ItemsPagePro page = new ItemsPagePro(sortedIte
方法
返回一个列表
来设置列表视图
的项目源
。
下面是嵌套列表的确切调用
下面是选项卡页面的调用
public ItemsMainTabbedPage(ObservableCollection<Titles> sortedItems)
{
InitializeComponent();
for (int i = 0; i < 3; i++)
{
ItemsPagePro page = new ItemsPagePro(sortedItems, i);
this.Children.Add(page);
}
}
它快把我逼疯了。当switcher
更改为0、1、2时,如果(switcher!=0)
,则不会执行此部分。我可以看到switcher
正在从方法的返回更改为sortedItemMain
。但是,在此处添加项时,项为空。添加(itemsSource[i][j])代码>。即使我删除复选框以强制添加项目,列表仍然为空
编辑:正如我发现的,这一行把事情搞砸了。itemsSource[i][j]。提示=。。。当我尝试更改它的值(字符串)时,没有任何错误,它会将其余部分弄乱您的代码没有编译observedcollection
没有像itemsSource[i][j]
Titles中有一个observedcollection这样的索引器访问权限。我发现,这一行弄乱了它。itemsSource[i][j]。提示=。。。当我尝试更改它的值(字符串)时,没有任何错误,它会把其余的搞乱
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;
}
}