当引发ErrorsChanged事件时,INotifyDataErrorInfo出现ArgumentOutOfRangeException错误。

11

这个问题比较奇怪,我认为它可能与我的机器配置有关。

基本上,我创建了一个相当标准的INotifyDataErrorInfo实现,在某些情况下,当引发ErrorsChanged事件时,我会收到一个ArgumentOutOfRangeException。此异常没有提供太多信息; 它给出了ArgumentOutOfRangeException crossed a native/managed boundary以及关于非负索引和集合大小的标准ArgumentOutOfRangeException描述。 InnerException为空。堆栈跟踪如下:

at System.ThrowHelper
        .ThrowArgumentOutOfRangeException(ExceptionArgument argument,
                                           ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Collections.ObjectModel.Collection`1.get_Item(Int32 index)
at System.Collections.ObjectModel.ReadOnlyCollection`1.get_Item(Int32 index)
我提到我的机器配置是因为我尝试了一些发布在博客上的解决方案(例如这里这里),并且遇到了相同的问题(即不是我的代码,而是INotifyDataErrorInfo的另一个实现),而在评论中没有任何其他人发现我遇到的问题。谷歌搜索出现了几个无关的结果。

需要的状态如下:

  1. 我输入到控件中的值会触发验证错误(这很好,错误文本按预期在界面上显示)。
  2. 然后我输入一个新值到控件中,验证将成功,并从错误集合中删除错误(HasErrors返回false)。
  3. ErrorsChanged被引发以反映不带错误的成功验证的更改,并出现异常。

更新:如果我将焦点移开显示验证错误的TextBox,也可以复现此问题。

我有点想知道是否错过了服务包/更新之类的东西,因为从我的看法来看,在框架代码中有一个非常基本的错误,同时,它对于其他人来说并没有发生。

更新:我使用的是Silverlight 4的最终RTM版。不是RC或Beta。

更新:我在这篇白皮书中提供的官方MS示例中也得到了相同的结果。

更新:我现在已经在另一台机器上测试了我的代码和提到的样本,一切正常。尽管如此,我仍然希望解决这个问题,因为它没有在我的常规机器上工作(直到现在我还没有遇到任何问题)。如果您有任何建议可以追踪导致此问题的原因,将不胜感激。我在问题机器上重新安装了Silverlight(Runtime、SDK、Toolkit),但问题仍然存在。

更新:以下是启用MS符号服务器的源服务器支持获取的框架代码中异常发生的调用堆栈:

mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument argument, System.ExceptionResource resource) + 0x40 bytes 
  mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException() + 0x10 bytes 
  mscorlib.dll!System.Collections.Generic.List<System.Windows.Controls.ValidationError>.this[int].get(int index = 0) + 0x13 bytes 
  mscorlib.dll!System.Collections.ObjectModel.Collection<System.Windows.Controls.ValidationError>.this[int].get(int index) + 0x2e bytes 
  mscorlib.dll!System.Collections.ObjectModel.ReadOnlyCollection<System.Windows.Controls.ValidationError>.this[int].get(int index) + 0x2e bytes 
  [Native to Managed Transition] 
  [Managed to Native Transition] 
  System.Windows.dll!System.Windows.IndexerListener.Value.get() + 0xc3 bytes 
  System.Windows.dll!System.Windows.IndexerPathStep.Value.get() + 0x17 bytes 
  System.Windows.dll!System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(System.Windows.PropertyPathStep source = {System.Windows.IndexerPathStep}) + 0x17 bytes 
  System.Windows.dll!System.Windows.IndexerPathStep.RaisePropertyPathStepChanged(System.Windows.PropertyListener source) + 0xe bytes 
  System.Windows.dll!System.Windows.IndexerListener.SourcePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs args) + 0xea bytes 
  System.Windows.dll!System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(object sender, System.ComponentModel.PropertyChangedEventArgs args) + 0x3d bytes 
  System.Windows.dll!System.Collections.ObjectModel.ReadOnlyObservableCollection<System.__Canon>.OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args) + 0x17 bytes 
  System.Windows.dll!System.Collections.ObjectModel.ReadOnlyObservableCollection<System.__Canon>.HandlePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) + 0xe bytes 
  System.Windows.dll!System.Collections.ObjectModel.ObservableCollection<System.Windows.Controls.ValidationError>.OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e) + 0x37 bytes 
  System.Windows.dll!System.Collections.ObjectModel.ObservableCollection<System.Windows.Controls.ValidationError>.RemoveItem(int index = 0) + 0x79 bytes 
  mscorlib.dll!System.Collections.ObjectModel.Collection<System.Windows.Controls.ValidationError>.Remove(System.Windows.Controls.ValidationError item) + 0x75 bytes 
  System.Windows.dll!System.Windows.Controls.Validation.RemoveValidationError(System.Windows.FrameworkElement fe = {System.Windows.Controls.TextBox}, System.Windows.Controls.ValidationError error) + 0x40 bytes 
  System.Windows.dll!System.Windows.Data.BindingExpression.RemoveErrorFromTarget(System.Windows.Controls.ValidationError error) + 0x48 bytes 
  System.Windows.dll!System.Windows.Data.BindingExpression.NotifyOldDataErrorInfos(System.Collections.ObjectModel.Collection<System.Windows.Controls.ValidationError> validationErrors) + 0x73 bytes 
  System.Windows.dll!System.Windows.Data.BindingExpression.NotifyOldDataErrorInfos(bool isNotifyChildDataErrorInfo) + 0x25 bytes 
  System.Windows.dll!System.Windows.Data.BindingExpression.NotifyDataErrorInfo_ErrorsChanged(object sender, System.ComponentModel.DataErrorsChangedEventArgs e) + 0xad bytes 
更新: 应用程序在未附加调试器的情况下正常运行(在出问题的计算机上),按预期工作,并且不会调用任何未处理的异常脚本(这让我困惑,难道与VS有关?)。 我快速搜索了一下谷歌,看看是否可以找到任何可记录此处信息的Silverlight插件日志文件,但没有成功,是否存在这样的日志文件?

我在VS2012上使用SL5遇到了相同的错误。 - Konstantin Salavatov
你找到答案了吗?我没有使用Silverlight也遇到了类似的问题。https://dev59.com/_3DYa4cB1Zd3GeqPBYvG - Kirsten
@kirsten 不好意思,没有找到解决办法。只在附有调试器的那台机器上重现过此问题,所以从未找到是什么原因。 - Simon Fox
我有同样的问题 - 没有SilverLight,只有普通的WPF。它实际上没有抛出异常,或者至少没有一个我可以捕获的异常,但它确实引发了一个绑定错误,如下所示:(0)[0].ErrorContext. failed to get value. ArgumentOutOfRangeException:'SystemArgumentOutOfRangeException: Specified argument was out of range of valid values. 这真的很烦人,因为我希望我的代码不会有绑定错误。 - Dave
3个回答

3
我认为这是文本框模板工具提示中的绑定错误。
{Binding (Validation.Errors)[0].ErrorContent}

验证错误已经清空,但是绑定仍然没有更新其引用。

我建议忽略异常。在调试设置中取消选中“当异常跨越AppDomain或管理时中断...”。


我在我的电脑上尝试了一下,它完美地运行了。我有点怀疑这是否会在其他类型的异常上引起其他问题,但我检查了我的三个同事安装的VS 2010,他们默认都没有选中这个选项。所以我想这确实是解决这个问题的正确方法。 - Nikos Tsokos

0

我成功修复了这个问题,方法是从 Web 项目中删除 Silverlight 应用链接,然后重新添加。

希望这能有所帮助。


很抱歉听到这个消息。奇怪的是,我没有提交更改,而是通过撤销Web项目上的待处理更改并使用原始文件进行重建来进行实验,令我惊讶的是问题没有再次出现。我还取消了(然后重新选中)Web项目中的Debug选项,并在解决过程中对RIA链接到项目进行了相同的操作。每次我都重新构建整个解决方案。这很奇怪,因为我只在新安装的机器上发现了这个错误。 - Chooksii

0

在这种情况下,本地代码可能会抛出两种不同的异常。无论您在 catch 中放置什么(或不放置任何内容),都无法捕获它们。

其中一种是“ArgumentOutOfRangeException crossed a native/managed boundary”。要停止看到此异常,您需要在“工具”>“选项”>“调试”>“常规”中取消选中“跨 AppDomain 或托管/本机边界时中断异常(仅限托管)”。

另一种异常只是“ArgumentOutOfRangeException”。要停止看到此异常,您需要在“工具”>“选项”>“调试”>“常规”中选中“启用我的代码”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接