我有一个存储在editor.tabs
中的Tab对象的集合。我试图从列表中除去一个标签。这里是我的代码看起来像现在,它不显而易见的原因工作:管理更改集合
// We need to make a copy of editor.tabs because other foreach fails
// since CloseTab actually removes tabs from editor.tabs.
// Basically, during the enumeration of a collection, that collection
// cannot change. So we just iterate over pointers instead.
List<Tab> tempList = new List<Tab>();
foreach (Tab tab in editor.tabs)
{
tempList.Add(tab);
}
foreach (Tab tab in tempList)
{
//Close everything but the current tab
if (tab != editor.currentTab)
{
CloseTab(tab);
}
}
editor.currentTab
距离CloseTab
修改,因此询问是否tab = editor.currentTab
没有提出正确的问题。我们希望在第一次进入该功能时询问tab是否等于editor.currentTab
的值。
我曾考虑过制作editor.currentTab
的深层拷贝,但这种感觉过度。
我也试着计算editor.currentTab
的散列,当我们第一次输入函数,然后将它与tab.GetHashCode()
比较,但也没有工作。有人有想法吗?
编辑:
我只是意识到错误是在我CloseTab功能!哎呀!
你在做什么'CloseTab'方法? – Habib
有两件重要的事情:editor.currentTab指向一个位于editor.tabs中的Tab对象。我们从editor.tabs中删除这个Tab对象,然后将editor.currentTab重新分配给editor中的一个不同的Tab对象.tabs – Nick
那么你在哪里存储对你想保留的标签的引用?我无法理解如果关闭Tab的条件是否与editor.currentTab不相等,editor.currentTab如何关闭。 –