naishx naishx - 2 months ago 13
C# Question

WeakSubscribe in MvvmCross dosn't work correct


I have an unknown Model-Object wich contains one or more ObservableCollections.
These collections contains objects wich implements INotifyPropertyChanged.
I now want to observe the PropertyChangedEvents for all objects inside the Model-Object.

What i have done:

So i wrote this methode wich 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 its not working.

Thanks for Help

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)

foreach (var inpc in collection.Cast<INotifyPropertyChanged>())
tempTokenList.Add(inpc.WeakSubscribe((sender, e) => DetailDataPropertyChanged(e.PropertyName)));
_subscriptionTokens = tempTokenList.ToArray();

// This methode 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:


with the method signature changed to:

private void DetailDataPropertyChanged(object sender, PropertyChangedEventArgs e)