I was just looking for the same answer, however my class is also an event sender for WPF, so it should look just like a regular C # / WPF event sender class. So I just added this:
To sender:
- enum with property name is a lame workaround due to missing name
- one additional method for recording requests
To the recipient:
- event request for this listing
Code, sender:
public enum Properties { NetworkFileName, DatasetFileName, LearningWatch } private string network_filename; public string NetworkFileName { get { return network_filename; } private set { if (network_filename != value) { network_filename = value; OnPropertyChanged(Properties.NetworkFileName.ToString()); } } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } public void OnChange(Properties prop, Action<object, PropertyChangedEventArgs> action) { PropertyChanged += new PropertyChangedEventHandler((obj, args) => { if (args.PropertyName == prop.ToString()) action(obj, args); }); }
And to the recipient:
private void OnNetworkLoaded(object sender, PropertyChangedEventArgs e) { SetTitle(); } ... ExpManager.OnChange(ExperimentManager.Properties.DatasetFileName, OnDatasetLoaded);
This is still ugly, but at least:
- I do not need to deal with "ifs" in the receiver
- I can easily create multiple event handlers
- It is compatible with WPF
- no magic lines (I hate them)
Inconvenience:
- obsfuscation destroys this (but I have a special class for this case, this project is only for me, so there is no problem here)
source share