weak event pattern Silverlight 3
Can someone tell me if silverlight 3 controls use the weak event pattern. So, if I write:
myView.Listbox1.ItemSource = MyView.ViewModel.SomeObservableCollection;
or
MyVie开发者_运维技巧w.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?
Also, what about events that I explicitly add a handler to in the view constructor, such as:
MyView() { InitializeComponent(); MyView.ViewModel.OnPropertyChanged += new PropertyChangedEventHandler(model_propertyChanged); }
Is there a simple way of using a weak reference here?
Thanks, Manish
ListBox and DataGrid will detach a handler from NotifyCollectionChanged
when necessary as well as using weak references via a small mediator object. Hence memory leakage whilst possible is very small and is completely eliminated whenever NotifyCollectionChanged actually fires.
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.
精彩评论