如何删除 CompositeWPF/Silverlight 中区域中的所有视图

发布于 2024-08-25 11:27:29 字数 7264 浏览 3 评论 0原文

我想删除 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);
        }

这是我的调用堆栈:

[外部代码]
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(对象 发件人 = 计数 = 0, System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) 第 153 行 + 0xb 字节 C# [外部代码]
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Region.Remove(对象 视图 = {Kite.MyApp.SlUI.NavigationRegion.NavigationRegionView}) 线 230 + 0x1b 字节 C#
MyApp.SlUI!Kite.MyApp.SlUI.Components.ViewController.linksRegion_LanguageChanged(对象 发送者= {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(对象 发送者= {System.Windows.Controls.ComboBox}, System.Windows.Controls.SelectionChangedEventArgs e = {System.Windows.Controls.SelectionChangedEventArgs}) 第 31 行 + 0xb bytes C# [外部代码]

我做错了什么?

PS 在我所在的地区,我有 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#

I want remove all views from my region in Composite. I'm using Silverlight 4.0.

And unfortunately, this code crashes with OutOfRangeException inside Composite.

        List<object> views = new List<object>(_regionManager.Regions["NavigationRegion"].Views);

        foreach (object view in views)
        {
            _regionManager.Regions["NavigationRegion"].Remove(view);
        }

This is my callstack:

[External Code]
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# [External Code]
Microsoft.Practices.Composite.Presentation!Microsoft.Practices.Composite.Presentation.Regions.Region.Remove(object
view = {Kite.MyApp.SlUI.NavigationRegion.NavigationRegionView}) Line
230 + 0x1b bytes C#
MyApp.SlUI!Kite.MyApp.SlUI.Components.ViewController.linksRegion_LanguageChanged(object
sender = {Kite.MyApp.SlUI.Controls.LinksUserControl},
Kite.MyApp.SlUI.Common.EventArgs e =
{Kite.MyApp.SlUI.Common.EventArgs}) Line 77 + 0x2d bytes C#
MyApp.SlUI!Kite.MyApp.SlUI.Controls.LinksUserControl.OnLanguageChanged(string
newLanguage = "en-GB") Line 37 + 0x32 bytes C#
MyApp.SlUI!Kite.MyApp.SlUI.Controls.LinksUserControl.ComboBox_SelectionChanged(object
sender = {System.Windows.Controls.ComboBox},
System.Windows.Controls.SelectionChangedEventArgs e =
{System.Windows.Controls.SelectionChangedEventArgs}) Line 31 + 0xb
bytes C# [External Code]

What did I wrong?

P.S. In my region I have 3 views. So, composite crashes on removing the third.

Hoping for you help :). Thank you.

UPD: I downloaded debug symbols and found that problem is only in Accordion control as region.
This is full call stack:

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#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傾城如夢未必闌珊 2024-09-01 11:27:40

如果你只想删除一个,你可以这样做:

 _regionManager.Regions["NavigationRegion"].Remove(
                    _regionManager.Regions["NavigationRegion"].GetView("viewName"));

if you want to remove just one, you could do:

 _regionManager.Regions["NavigationRegion"].Remove(
                    _regionManager.Regions["NavigationRegion"].GetView("viewName"));
幽梦紫曦~ 2024-09-01 11:27:37

修改集合时使用 for 循环而不是 foreach

use a for loop not foreach when modifying a collection

绾颜 2024-09-01 11:27:36

首先检查 _regionManager.Regions["NavigationRegion"] 是否确实存在并且不为 null,如下所示

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 设置不正确,或者存在拼写错误或类似的情况

Check first that _regionManager.Regions["NavigationRegion"] actually exists and is non null like so

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);
        }
}

It may be that NavigationRegion isnt set up properly, or there is a typo or something like that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文