简体   繁体   English

弱事件模式Silverlight 3

[英]weak event pattern Silverlight 3

Can someone tell me if silverlight 3 controls use the weak event pattern. 有人可以告诉我Silverlight 3控件是否使用了弱事件模式。 So, if I write: 所以,如果我写:

myView.Listbox1.ItemSource = MyView.ViewModel.SomeObservableCollection; myView.Listbox1.ItemSource = MyView.ViewModel.SomeObservableCollection;

or 要么

MyView.DataGrid1.ItemSource = MyView.ViewModel.SomeOtherObservableCollection; MyView.DataGrid1.ItemSource = MyView.ViewModel.SomeOtherObservableCollection;

And the ViewModel here is a singleton that lives much longer than the view itself, will I get memory leaks or do the ListBox and Datagrid controls in silverLight 3 already take care of this? 而且这里的ViewModel是一个单例,其寿命比视图本身长得多,我是否会发生内存泄漏,或者silverLight 3中的ListBox和Datagrid控件已经解决了这一问题?

Also, what about events that I explicitly add a handler to in the view constructor, such as: 另外,关于我在视图构造函数中显式添加处理程序的事件呢,例如:

MyView() { InitializeComponent(); MyView(){InitializeComponent(); MyView.ViewModel.OnPropertyChanged += new PropertyChangedEventHandler(model_propertyChanged); MyView.ViewModel.OnPropertyChanged + =新的PropertyChangedEventHandler(model_propertyChanged); } }

Is there a simple way of using a weak reference here? 这里有使用弱引用的简单方法吗?

Thanks, Manish 谢谢,Manish

ListBox and DataGrid will detach a handler from NotifyCollectionChanged when necessary as well as using weak references via a small mediator object. 必要时,ListBox和DataGrid会从NotifyCollectionChanged分离处理程序,并通过小的中介对象使用弱引用。 Hence memory leakage whilst possible is very small and is completely eliminated whenever NotifyCollectionChanged actually fires. 因此,尽管可能发生的内存泄漏非常小,并且每当NotifyCollectionChanged实际触发时,内存泄漏就完全消除了。

The pattern to do this yourself is to use the Mediator pattern and to have the Mediator object hold a weak reference to one of the parties involved. 自己执行此操作的模式是使用Mediator模式,并使Mediator对象对所涉及的各方之一持有弱引用。

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

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