在进行编辑的情况下切换视图时,NotifyPropertyChanged 中引发 NullReferenceException

发布于 2024-09-26 08:14:22 字数 605 浏览 0 评论 0原文

我有一系列视图,这些视图将根据父视图中树中当前选定的项目进行显示。这些视图是在父视图的初始化方法期间创建并注册到该区域的,并且正在正确地停用/激活,因此给出了换入/换出正确视图的效果。这些视图具有单个底层视图模型作为其数据上下文,其中包含支持 INotifyPropertyChanged 的​​数据对象。

如果子视图中当前没有正在进行的未完成的编辑,但如果视图中有正在进行的编辑(即用户已更改描述的内容但尚未单击文本框),则此解决方案有效该视图被停用(即,在父视图中单击不同的树项,从而导致发生停用)在附加到现在停用的视图的基础数据对象的 NotifyPropertyChanged() 中抛出 NullReferenceException。

似乎发生的事情是这样的:

  • 用户在子视图中启动编辑
  • 用户单击父视图中树中的项目
  • 控制器拾取树中所选项目的更改
    • 当前子视图已停用
    • 新视图已激活
  • 基础数据对象发生编辑更改(调用 set 方法)
  • 由于此更改,数据对象会生成更改通知事件
  • 抛出空引用异常。

据推测,此更改通知事件正在发送到现在已停用的视图,但该视图不为空。

I have a series of views that are to be displayed depending on the currently selected item in a tree within a parent view. These views are created and registered with the region during the initialization method of the parent view and are being correctly deactivated/activated, therefore giving the effect of swapping in/out the correct view. These views have a single underlying viewmodel as their datacontext, which contains data objects supporting INotifyPropertyChanged.

This solution works if there are no currently outstanding edits in progress within the child view but if there is a edit in progress in a view (i.e. the user has changed the contents of a description but hasn't clicked out of the text box) and that view is deactivated (i.e. the a different tree item is clicked within the parent view, thus causing a de-activation to occur) a NullReferenceException is being thrown in the NotifyPropertyChanged() of the underlying data object attached to the now deactivated view.

What seems to be happening is this:

  • An edit is started by the user in the child view
  • The user clicks an item in the tree in the parent view
  • The controller picks up the change in the selected item in the tree
    • The current child view is deactivated
    • The new view is activated
  • The change from the edit happens to the underlying data object (the set method is getting called)
  • A change notification event is generated by the data object as a result of this change
  • A null reference exception is thrown.

Presumably, this change notification event is being sent to the now de-activated view, but the view is not null.

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

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

发布评论

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

评论(1

咽泪装欢 2024-10-03 08:14:22

我自己没有尝试过,但我相信一种解决方案是使用 IActiveAware 监听视图的停用事件并取消任何编辑。

查看此链接 有帮助。

I have not tried this myself, but I believe one solution was to listen for the deactivate event of the view using IActiveAware and cancel any editing.

See if this link helps.

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