未处理 WPF 用户控件 [英] WPF user control not being disposed

查看:61
本文介绍了未处理 WPF 用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次打开视图时,对每个模型属性的 get 请求数都会增加 1.只是要清楚,如果我打开视图一次,关闭它并再次打开它,每个属性都有两个 get 请求,如果属性是 setRaisePropertyChanged("propertName") 再次触发了两个 get 请求.get 请求的次数总是等于视图被实例化的次数!我已经调试了这个问题的生命,一切都表明当视图关闭并且绑定被保留时,用户控件没有被处理.

Each time I open a view the number of get requests to each of the model properties increment by one. Just to be clear if I open the view once, close it and open it again there are two get requests to each property, and if the property is set and RaisePropertyChanged("propertName") is triggered there are two get requests again. The number of get requests is always equal to the number of times the view has been instantiated! I have debugged the life out of this issue and everything points towards it being that the user controls aren't being disposed of when the view is closed and the bindings are withheld.

我该怎么办?我已经研究过用户控件的处理,但是我没有成功找到帮助我解决这个问题的东西.也许我没有在寻找合适的主题?任何帮助或指示将不胜感激 - 谢谢!

What should I do to deal with this? I have looked into disposal of user controls however I have been unsuccessful in finding something to help me fix this. Maybe I am not looking for the right topic? Any help or pointers would be greatly appreciated - thanks!

推荐答案

WPF 控件不实现 IDisposable,因此不需要处理.我相信你想说没有被标记为符合垃圾收集条件"

WPF controls do not implement IDisposable and, hence, don't need to be disposed of. I believe that you want to say "is not being marked as eligible to garbage collection"

您的问题似乎与所谓的事件引用有关.您的某些活动实例(您拥有的某些类不是您的窗口/用户控件)保留对事件的引用.如果您关闭 Window 或 UserControl,该链接仍然存在并且不会自动清除.

it seems that your problem is related to what is called an event reference. Some of your living instance (some class you have which is not your window/user control) retains a reference to an event. If you close the Window or UserControl that link still lives on and it is not cleared automatically.

关闭窗口/用户控件时,您应该像这样取消引用您的事件

When closing the window/user control you should dereference your event like this

EventName-= methodHandler或者this.UserControlInstance=null

您可以在这里阅读一些有趣的模式

这篇关于未处理 WPF 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆