2012-11-19 28 views
0

我通過刪除和添加所需位置的元素(插入方法)來交換堆棧面板中的兩個子元素。但刪除並添加元素會導致內存地址問題和應用程序崩潰,只要我訪問Element swapped.Is有任何其他方式,我可以實現交換沒有「刪除和添加」在StackPanel?重新排序StackPanel中的孩子? Win8

Win8 + XAML + C#。

代碼片段:

UIElementCollection children = stack_map.Children; 

    int i = children.IndexOf(stack_listView);         
    StackPanel sp = children[i] as StackPanel; 

    int j = children.IndexOf(mapPageInstance);         
    MapPage mp = children[j] as MapPage; 

    stack_map.Children.Remove(sp); 
    stack_map.Children.Insert(1, sp); 

我想從零索引位置 'SP' 在stack_map第一指標。

「mapPageInstance」是一個自定義地圖類(基本地圖)

+0

你能張貼代碼片段,其說明什麼問題?刪除和重新插入元素不應導致任何問題 –

+0

編輯和添加片段。 – Suny

回答

0

的實例,以交換在上面你StackPanel中的元素,我只是使用以下命令:

stack_map.Children.Remove(stack_listView); 
stack_map.Children.Insert(1, stack_listView); 

當然,這意味着你的堆棧面板中有多個物品。

不應有交換後的任何問題/內存錯誤,如果你繼續的名字,而不是通過索引來訪問的StackPanel項目

+0

does not work this way either.Crash happen in touch mode of simulator and on Surface but not on Desktop.Strange.Any other suggestion? – Suny