0
votes

I have following problem using MVVM Light with universal app in wp8.1. In my ViewModel in constructor I create a simple RelayCommand with lambda instead of reference to method. The problem is, that sometimes when I navigate to Desktop on phone and then back to app -- the Weak reference is gone. I do understand the reason for using weak references, but I don't understand why does it removes when not unloading View and ViewModel. The same happens when returning to the page through Frame.GoBack. Even without page caching (the new instance created then) the reference suddenly sets to collected.

GoHomeCommand = new RelayCommand(() =>
        {
            navigationService.NavigateTo("ScheduleChoicePage");
        });

 <AppBarButton x:Uid="HomePage" Command="{Binding GoHomeCommand}"/>

It seems though, that when using reference to method, not lambda -- there's no problem.

1

1 Answers

0
votes

Actually releasing of this reference is not a matter of loading/unloading/navigation. It's only a matter of garbage collector. There are no strong references to this lambda so it can be collected any time after you created it.

Solution is not to use lambdas when creating actions or keep lambda reference somewhere in your ViewModel object.