Context:
I have an unknown Model-Object which contains one or more ObservableCollections. These collections contains objects which implements INotifyPropertyChanged. I now want to observe the PropertyChangedEvents for all objects inside the Model-Object.
What I have done:
So I wrote this method which uses reflection to find the specific objects. This all works fine. Except the part where it comes to the MvvmCross function WeakSubscribe. I really like the idea behind, but it seems it loses the reference and don't fire the event.
Strange: If I debug this code it works correct, but without breakpoints it's not working.
private void SubscribeToDetailData()
{
var tempTokenList = new List<MvxNotifyPropertyChangedEventSubscription>();
var fieldInfos =
DetailData.GetType().GetRuntimeProperties().Where(f => Helpers.IsSubclassOfRawGeneric(typeof (ObservableCollection<>), f.PropertyType));
foreach (var fieldInfo in fieldInfos)
{
var collection = fieldInfo.GetValue(DetailData) as IEnumerable<object>;
if (collection == null)
continue;
foreach (var inpc in collection.Cast<INotifyPropertyChanged>())
{
tempTokenList.Add(inpc.WeakSubscribe((sender, e) => DetailDataPropertyChanged(e.PropertyName)));
}
}
_subscriptionTokens = tempTokenList.ToArray();
}
// This method is never raised
private void DetailDataPropertyChanged(string propertyName)
{
if (_enabledFields.Evaluate(DetailData, propertyName))
RaisePropertyChanged(() => FieldEnabledState);
}
It might be that your subscribed Action
is getting garbage collected.
This can be caused, I think, if the compiler creates an instance of an anonymous class to implement your anonymous Action
. I wouldn't normally expect this to happen in your code because you are not using any local variables in your Action
- but this could be the case.
Does your code work if you change the subscription to:
tempTokenList.Add(inpc.WeakSubscribe(DetailDataPropertyChanged));
with the method signature changed to:
private void DetailDataPropertyChanged(object sender, PropertyChangedEventArgs e)