wpf PresentationFramework.dll 中发生了“System.InvalidOperationException”类型的第一次机会异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15414574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A first chance exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
提问by William
All,
全部,
I'm using DataBase First Entity Framework v4.4. In the DB (and data model) is Table1 that has a 1:many relationship to Table2.
我正在使用 DataBase First Entity Framework v4.4。在 DB(和数据模型)中是 Table1,它与 Table2 具有 1:many 关系。
I am binding a DataGrid in WPF to Table1.Local.First().Table2 (for simplicity, assume that there is an entity in Table1.Local to begin with).
我将 WPF 中的 DataGrid 绑定到 Table1.Local.First().Table2(为简单起见,假设 Table1.Local 中有一个实体开始)。
ViewModel:
视图模型:
Public SomeEntityDBContextWithTable1AndTable2 Container { get; set; }
Public ICollection Table2ToDisplay { get { return Container.Table1.Local.First().Table2; } } //Note: :Many navigation properties return ICollection<T>, but the object type is of ObservableCollection<T>.
In XAML, I have the following
在 XAML 中,我有以下内容
<GroupBox Header=Table2 DataContext="{Binding Path=Table2ToDisplay, UpdateSourceTrigger=PropertyChanged}">
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="True">
<DataGrid.Columns>
<!--A bunch of columns-->
</DataGrid.Columns>
</DataGrid>
</GroupBox>
When clicking inside a NewItemPlaceHolder that happens to be a text box, I get the System.InvalidOperationException occuring in PresentationFramework.dll. This doesn't crash my application, but I see it in the Output. My guess is that entities are added on another thread, and thus CollectionChanged event fires on another thread, and this causes the InvalidOperationException. However, since the code is mainly done via XML, I can't seem to find a way to handle this exception (or is it already being handled, it's just that it is being reported to Output). Is there a safe way to use CanUserAddRows="True" with EntityFramework where ":Many" navigation properties are of type ObservableCollection?
在恰好是文本框的 NewItemPlaceHolder 内部单击时,我在 PresentationFramework.dll 中出现 System.InvalidOperationException。这不会使我的应用程序崩溃,但我在输出中看到它。我的猜测是实体被添加到另一个线程上,因此 CollectionChanged 事件在另一个线程上触发,这会导致 InvalidOperationException。但是,由于代码主要是通过 XML 完成的,我似乎无法找到处理此异常的方法(或者它是否已经被处理,只是它正在报告给输出)。是否有一种安全的方法可以将 CanUserAddRows="True" 与 EntityFramework 一起使用,其中 ":Many" 导航属性的类型为 ObservableCollection?
I should point out that I have also tried wrapping my Table2ToDisplay property inside a CollectionViewSource, but I still see the InvalidOperationException in the output.
我应该指出,我还尝试将 Table2ToDisplay 属性包装在 CollectionViewSource 中,但我仍然在输出中看到 InvalidOperationException。
Thanks in advance.
提前致谢。
回答by JerKimball
I'm going to take a wild guess since not all the details are present (update code, stack trace, etc) that you are changing the bound collection in a non-ui thread.
我将进行一个疯狂的猜测,因为并非所有细节都存在(更新代码、堆栈跟踪等),您正在更改非 ui 线程中的绑定集合。
In which case, you need:
在这种情况下,您需要:
(in update logic)
(在更新逻辑中)
Application.Current.Dispatcher.Invoke((Action)(() =>
{
// update collection here
});
回答by William
Okay. Changing Visual Studio's debugger to stop at all exceptions lead me to some insite. The exception was "NewItemPlaceHolder is not allowed during an AddNew...", which occured because of a PropertyChanged event on my Entity. Turns out this exception was already being handled, so I think I'm good.
好的。更改 Visual Studio 的调试器以在所有异常时停止,这让我进入了一些现场。异常是“在 AddNew...期间不允许 NewItemPlaceHolder”,这是由于我的实体上的 PropertyChanged 事件而发生的。原来这个异常已经被处理了,所以我觉得我很好。

