iSeller Commerce
iSeller POS Retail
iSeller POS F&B
iSeller POS Express
Crosslight
WebUI
ClientUI
What's New
Download Trial
Web Solution
Mobile Solution
Enterprise Solution
Custom Development
Blog
Community
Latest Development Blogs
ForumPostTopic
Browse By Tag
Hi
can you please explain the code (bolded) - thanks
// Selection, View States public ContactViewModel SelectedItem { get { return _selectedItem; } set { if (_selectedItem != value) { if (_selectedItem != null) _selectedItem.Contact.PropertyChanged -= new PropertyChangedEventHandler(Contact_PropertyChanged); if (value != null) value.Contact.PropertyChanged += new PropertyChangedEventHandler(Contact_PropertyChanged); _selectedItem = value; OnPropertyChanged("SelectedItem"); OnPropertyChanged("SelectionStatus"); InvalidateCommands(); } } }
... if (value != null) value.Contact.PropertyChanged += new PropertyChangedEventHandler(Contact_PropertyChanged); ...
The above code will subscribe to a PropertyChanged event for value.Contact object if value doesn’t equal to null (if condition is met).
... if (_selectedItem != null) _selectedItem.Contact.PropertyChanged -= new PropertyChangedEventHandler(Contact_PropertyChanged); ...
The code will unsubscribe from a PropertyChanged event for selectedItem.Contact object if selectedItem doesn’t equal to null.
For more detail information, you may check the “How to: Subscribe to and Unsubscibe from Events”.
Hope this help.
I understand the code as you describe (subscribing/unsubscribing) what I wanted to know is why does it have to subscribe and unsubscribe when the selected item changes
If I’m not mistaken, the snippet code is available in the Contacts MVVM C# Sample project (start > All Programs > Intersoft WebUI Studio 2012 R1 SP1 > WebUI Studio for Silverlight 5 > Development Samples > Contacts MVVM C# Sample).
The subscribe and unsubscribe of PropertyChanged event are done in the setter of SelectedItem property because the sample would like to inform the OnPropertyChanged of SelectionStatus during item selection.
We are nearly there...yes I understand adding the OnPropertyChanged but why do we have to remove as well under this logic:
if (_selectedItem != null) _selectedItem.Contact.PropertyChanged -= new PropertyChangedEventHandler(Contact_PropertyChanged);
In order to prevent resource leaks, you should unsubscribe from events before you dispose of a subscriber object. Until you unsubscribe from an event, the multicast delegate that underlies the event in the publishing object has a reference to the delegate that encapsulates the subscriber’s event handler. As long as the publishing object holds that reference, garbage collection will not delete your subscriber object.
When user selects an item, initially the _selectedItem is null and the value keep the information about what the user select. In this condition, the PropertyChanged event handler is subscribed. Next, _selectedItem is set to be equal to value. OnPropertyChanged(“SelectedItem”) will be raised and the setter of SelectedItem property will get fired again. Now, _selectedItem is no longer null, and we can use it to unsubscribe the event.
Please feel free to let us know your feedback or response.
or
Choose this if you're already a member of Intersoft Community Forum. You can link your OpenID account to your existing Intersoft Social ID.
Choose this if you don't have an Intersoft account yet. Your authenticated OpenID will be automatically linked to your new Intersoft account.
Enter your Wordpress Blogname