简体   繁体   English

WPF MVVM DataBindings停止更新

[英]WPF MVVM DataBindings stop updating

I am working on a medium size WPF application that utilizes the MVVM pattern. 我正在使用MVVM模式的中型WPF应用程序。 ViewModels use INotifyPropertyChanged to refresh their respective Views. ViewModels使用INotifyPropertyChanged刷新各自的视图。

This approach works perfectly, except for one problem: when this application is left running for long periods of time (3-7 days) the Views (every single View in the entire app!) suddenly stop updating their bound properties. 这种方法很有效,除了一个问题:当这个应用程序长时间运行(3-7天)时,视图(整个应用程序中的每个视图!)突然停止更新它们的绑定属性。

If I set a breakpoint in the ViewModels, they are chugging away happily, calling PropertyChanged like nothing is wrong. 如果我在ViewModels中设置了一个断点,那么他们就会愉快地开玩笑,调用PropertyChanged就像没有错。 However, if I set a breakpoint in the getter of one of the ViewModel objects that the View is bound to, the getter is never called! 但是,如果我在View绑定的ViewModel对象的getter中设置断点,则永远不会调用getter!

I am stumped at this point, and don't even know how to debug this issue properly. 我很难过,甚至不知道如何正确调试这个问题。 I have checked the Visual Studio output window for data binding errors, but everything looks normal. 我已检查Visual Studio输出窗口是否存在数据绑定错误,但一切看起来都很正常。 It is almost as if the WPF data binding engine has crashed in the background. 几乎就像WPF数据绑定引擎在后台崩溃一样。 This app is also monitoring unhandled exceptions ( AppDomain.UnhandledException and Dispatcher.UnhandledException ), but no exceptions are being thrown. 此应用程序还监视未处理的异常( AppDomain.UnhandledExceptionDispatcher.UnhandledException ),但不会抛出任何异常。

Summary: After long periods of time, the Views stop updating their data bindings, but the ViewModels are still calling the PropertyChanged event. 简介:经过很长一段时间后,Views停止更新其数据绑定,但ViewModel仍在调用PropertyChanged事件。

Any advice??? 任何建议???

After several months of debugging, I was finally able to solve the issue by attaching a debugger to the remote target. 经过几个月的调试,我终于能够通过将调试器附加到远程目标来解决问题。 Only then did it produce an exception that I could debug. 只有这样才能产生我可以调试的异常。 I still do not understand why AppDomain.UnhandledException and Dispatcher.UnhandledException did not catch this exception, but at least I was able to figure it out. 我仍然不明白为什么AppDomain.UnhandledException和Dispatcher.UnhandledException没有捕获到这个异常,但至少我能够弄明白。

You might be falling victim to over-zealous weak references. 你可能会成为过度热心的弱引用的牺牲品。 There might be a fix for your MVVM framework. 您的MVVM框架可能有一个修复程序。 If not, this information might help you find the issue in the source code. 如果没有,此信息可能会帮助您在源代码中找到问题。

There is a known memory leak in most implementations of INotifyPropertyChanged. 在大多数INotifyPropertyChanged实现中存在已知的内存泄漏。 The view model takes a hard reference on the delegate of the XAML control's PropertyChanged handler. 视图模型对XAML控件的PropertyChanged处理程序的委托进行了硬引用。 That delegate in turn takes a hard reference on the XAML control. 该代表反过来对XAML控件进行了硬性参考。 Because of this, the control can't be collected as long as the view model exists. 因此,只要视图模型存在,就无法收集控件。

So to fix this problem, many MVVM frameworks use weak references for events . 因此,要解决此问题,许多MVVM框架都会对事件使用弱引用 While this can fix the memory leak, it can also cause the problem that you are seeing. 虽然这可以解决内存泄漏问题,但它也可能导致您遇到的问题。 If the weak event isn't properly implemented, it might be removed before the XAML control is actually collected. 如果未正确实现弱事件,则可能在实际收集XAML控件之前将其删除。

I suspect that you are using an MVVM framework with weak references, and that they are getting prematurely disposed. 我怀疑你使用的是带有弱引用的MVVM框架,并且它们过早地被处理掉了。 To see if this is a likely issue, put a few calls to GC.Collect() and see if the problem occurs more frequently. 要查看这是否是一个可能的问题,请对GC.Collect()进行一些调用,然后查看问题是否更频繁地发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM