元素已经是另一个元素的 child - WinRT
Element is already the child of another element - WinRT
我需要开发一个类似于Windows8 开始屏幕的结构。我有一个方法,我调用它来填充我的 RecordTile
。方法就像,
public static void fnShowTiles(List<RecordTile> objList, Grid objGrid) // objGrid is root Grid which holds this structure
{
try
{
// copy the list into local variable
List<RecordTile> objListOfTiles = objList.ToList<RecordTile>();
int nColumnsCount = -1;
objGrid.Children.Clear();
objGrid.ColumnDefinitions.Clear();
StackPanel objPanel = new StackPanel();
int nOriginalListCount = objListOfTiles.Count;
int nRowsToRender = 4;
while (objListOfTiles.Count > 0)
{
// add new column to the grid
objGrid.ColumnDefinitions.Add(new ColumnDefinition());
nColumnsCount += 1;
// add new stackpanel to newly added column
objPanel = new StackPanel();
objGrid.Children.Add(objPanel);
Grid.SetColumn(objPanel, nColumnsCount);
// add elements to stackpanel
int i = 0;
while (i < nRowsToRender)
{
if (objListOfTiles.Count > 0)
{
// add first element and remove it from list, so that next element will be first
RecordTile tile = objListOfTiles.First();
objPanel.Children.Add(tile); // exception occurs here
objListOfTiles.Remove(tile);
i++;
}
else
{
// if while adding elements, list finishes, then break the loop
break;
}
}
}
}
这对于第一次加载工作正常。我在加载这些图块的同一页面上有一个 SearchBox
。当我过滤 Tiles(基于搜索字符串)并将新的 tiles 列表传递给函数时,它会抛出异常。
我浏览了很多帖子。他们建议从其 parent 中删除元素。我每次都在清除网格的children。一定是哪里出了问题?
正如@Rawling 所建议的那样,在 objGrid.Children.Clear();
之前从 StackPanel
中清除 Tiles 是解决方案。在该语句之前添加了以下代码,效果非常好。
foreach (StackPanel panel in objGrid.Children)
{
panel.Children.Clear();
}
错误很明显:A WPF/SL 控件也只能属于 time.Try 处的 1 个父控件。
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
foreach (StackPanel panel in objGrid.Children)
{
panel.Children.Clear();
}
}
我需要开发一个类似于Windows8 开始屏幕的结构。我有一个方法,我调用它来填充我的 RecordTile
。方法就像,
public static void fnShowTiles(List<RecordTile> objList, Grid objGrid) // objGrid is root Grid which holds this structure
{
try
{
// copy the list into local variable
List<RecordTile> objListOfTiles = objList.ToList<RecordTile>();
int nColumnsCount = -1;
objGrid.Children.Clear();
objGrid.ColumnDefinitions.Clear();
StackPanel objPanel = new StackPanel();
int nOriginalListCount = objListOfTiles.Count;
int nRowsToRender = 4;
while (objListOfTiles.Count > 0)
{
// add new column to the grid
objGrid.ColumnDefinitions.Add(new ColumnDefinition());
nColumnsCount += 1;
// add new stackpanel to newly added column
objPanel = new StackPanel();
objGrid.Children.Add(objPanel);
Grid.SetColumn(objPanel, nColumnsCount);
// add elements to stackpanel
int i = 0;
while (i < nRowsToRender)
{
if (objListOfTiles.Count > 0)
{
// add first element and remove it from list, so that next element will be first
RecordTile tile = objListOfTiles.First();
objPanel.Children.Add(tile); // exception occurs here
objListOfTiles.Remove(tile);
i++;
}
else
{
// if while adding elements, list finishes, then break the loop
break;
}
}
}
}
这对于第一次加载工作正常。我在加载这些图块的同一页面上有一个 SearchBox
。当我过滤 Tiles(基于搜索字符串)并将新的 tiles 列表传递给函数时,它会抛出异常。
我浏览了很多帖子。他们建议从其 parent 中删除元素。我每次都在清除网格的children。一定是哪里出了问题?
正如@Rawling 所建议的那样,在 objGrid.Children.Clear();
之前从 StackPanel
中清除 Tiles 是解决方案。在该语句之前添加了以下代码,效果非常好。
foreach (StackPanel panel in objGrid.Children)
{
panel.Children.Clear();
}
错误很明显:A WPF/SL 控件也只能属于 time.Try 处的 1 个父控件。
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
foreach (StackPanel panel in objGrid.Children)
{
panel.Children.Clear();
}
}