我想在Composite中删除我所在区域的所有视图。我正在使用Silverlight 4.0。
不幸的是,这段代码在Composite内部与OutOfRangeException崩溃。
List<object> views = new List<object>(_regionManager.Regions["NavigationRegion"].Views);
foreach (object view in views)
{
_regionManager.Regions["NavigationRegion"].Remove(view);
}
这是我的callstack:
[外部代码]
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) 102行+ 0x24字节C#
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.RemoveAndNotify(System.Collections.IList items = Count = 1)第45行+ 0x2b字节C#
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.UnderlyingCollection_CollectionChanged(对象 sender = Count = 0, System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) 第153行+ 0xb字节C#[外部代码]
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Region.Remove(对象 view = {Kite.MyApp.SlUI.NavigationRegion.NavigationRegionView})Line 230 + 0x1b字节C#
MyApp.SlUI!Kite.MyApp.SlUI.Components.ViewController.linksRegion_LanguageChanged(对象 sender = {Kite.MyApp.SlUI.Controls.LinksUserControl}, Kite.MyApp.SlUI.Common.EventArgs e = {Kite.MyApp.SlUI.Common.EventArgs})第77行+ 0x2d字节C#
MyApp.SlUI!Kite.MyApp.SlUI.Controls.LinksUserControl.OnLanguageChanged(串 newLanguage =“en-GB”)第37行+ 0x32字节C#
MyApp.SlUI!Kite.MyApp.SlUI.Controls.LinksUserControl.ComboBox_SelectionChanged(对象 sender = {System.Windows.Controls.ComboBox}, System.Windows.Controls.SelectionChangedEventArgs e = {System.Windows.Controls.SelectionChangedEventArgs})第31行+ 0xb 字节C#[外部代码]
我错了什么?
P.S。在我的地区,我有3个观点。因此,复合体在删除第三个时崩溃了。
希望你帮忙:)。谢谢。
UPD:我下载了调试符号,发现问题仅在Accordion控件中作为区域。 这是完整的调用堆栈:
mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument argument, System.ExceptionResource resource) + 0x41 bytes
mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException() + 0x10 bytes
mscorlib.dll!System.Collections.Generic.List<object>.this[int].get(int index = 0) + 0x13 bytes
System.Windows.dll!System.Windows.Controls.ItemCollection.GetItemImpl(int index) + 0x15 bytes
System.Windows.dll!System.Windows.Controls.ItemCollection.GetItemImplSkipMethodPack(int index) + 0xb bytes
System.Windows.dll!System.Windows.PresentationFrameworkCollection<System.__Canon>.this[int].get(int index) + 0xb bytes
System.Windows.Controls.Layout.Toolkit!System.Windows.Controls.Accordion.UnselectItem(int index = 0, object item = null) + 0x1aa bytes
System.Windows.Controls.Layout.Toolkit!System.Windows.Controls.Accordion.ChangeSelectedIndex(int oldIndex = 0, int newIndex = -1) + 0x14c bytes
System.Windows.Controls.Layout.Toolkit!System.Windows.Controls.Accordion.OnSelectedIndexPropertyChanged(System.Windows.DependencyObject d = {System.Windows.Controls.Accordion}, System.Windows.DependencyPropertyChangedEventArgs e = {System.Windows.DependencyPropertyChangedEventArgs}) + 0x1d9 bytes
System.Windows.dll!System.Windows.DependencyObject.RaisePropertyChangeNotifications(System.Windows.DependencyProperty dp = {System.Windows.CustomDependencyProperty}, object oldValue, object newValue) + 0x51 bytes
System.Windows.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation) + 0xf5 bytes
System.Windows.dll!System.Windows.DependencyObject.SetValueInternal(System.Windows.DependencyProperty dp, object value, bool allowReadOnlySet) + 0x2ad bytes
System.Windows.dll!System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty dp, object value) + 0xb bytes
System.Windows.Controls.Layout.Toolkit!System.Windows.Controls.Accordion.SelectedIndex.set(int value = -1) + 0x4d bytes
System.Windows.Controls.Layout.Toolkit!System.Windows.Controls.Accordion.OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) + 0x737 bytes
System.Windows.dll!System.Windows.Controls.ItemsControl.OnItemCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x52 bytes
System.Windows.dll!System.Windows.Controls.ItemCollection.NotifyCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x37 bytes
System.Windows.dll!System.Windows.Controls.ItemCollection.NotifyItemsCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x1f bytes
System.Windows.dll!System.Windows.Controls.ItemCollection.EnumerableCollectionView.System.Windows.Controls.ICollectionChangedListener.OnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x4f bytes
System.Windows.dll!System.Windows.Controls.WeakCollectionChangedListener.SourceCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x3d bytes
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) Line 102 + 0x24 bytes C#
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.RemoveAndNotify(System.Collections.IList items = Count = 1) Line 45 + 0x2b bytes C#
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.ViewsCollection.UnderlyingCollection_CollectionChanged(object sender = Count = 0, System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) Line 153 + 0xb bytes C#
System.Windows.dll!System.Collections.ObjectModel.ObservableCollection<Microsoft.Practices.Composite.Presentation.Regions.ItemMetadata>.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x37 bytes
System.Windows.dll!System.Collections.ObjectModel.ObservableCollection<System.__Canon>.RemoveItem(int index) + 0xa1 bytes
mscorlib.dll!System.Collections.ObjectModel.Collection<Microsoft.Practices.Composite.Presentation.Regions.ItemMetadata>.Remove(Microsoft.Practices.Composite.Presentation.Regions.ItemMetadata item) + 0x75 bytes
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Region.Remove(object view = {Kite.MyApp.SlUI.NavigationRegion.NavigationRegionView}) Line 230 + 0x1b bytes C#
答案 0 :(得分:5)
首先检查_regionManager.Regions [“NavigationRegion”]是否确实存在且非空如此
if (_regionManager.Regions["NavigationRegion"] != null)
{
List<object> views = new List<object>(_regionManager.Regions["NavigationRegion"].Views);
foreach (object view in views)
{
_regionManager.Regions["NavigationRegion"].Remove(view);
}
}
可能是NavigationRegion没有正确设置,或者有拼写错误或类似的东西
答案 1 :(得分:2)
在修改集合时使用for循环而不是foreach
答案 2 :(得分:1)
如果你只想删除一个,你可以这样做:
_regionManager.Regions["NavigationRegion"].Remove(
_regionManager.Regions["NavigationRegion"].GetView("viewName"));