Search code examples
c#windows-phone-8mvvm-lightunbindrelaycommand

windows phone 8 - memory leak causing interactivity commands


In Windows Phone 8 application I have ItemsControl with ItemTemplate which have event on tap:

 <interactivity:Interaction.Triggers>
     <interactivity:EventTrigger EventName="Tap"> 
         <command:EventToCommand Command="{Binding Mode=OneWay, Path=DataContext.NavigateToNextPage, Source={StaticResource Context}}" CommandParameter="{Binding}" />
     </interactivity:EventTrigger>
 </interactivity:Interaction.Triggers>-->

Context:

<ContentControl x:Key="Context" Content="{Binding}" />

RelayCommand:

public RelayCommand<MyItem> NavigateToNextPageCommand
{
    get { return _navigateToNextPageCommand ?? (_navigateToNextPageCommand = new RelayCommand<MyItem>(NavigateToNextPage)); }
}

ItemsControl is define:

<ItemsControl Grid.Row="2" ItemsSource="{Binding DepositsItems}">

DepositsItems is a List which has about 200 elements and I reload it sometimes. After few reloads I have memory leak and application closes. I found why it happens. When I remove Tap event, everything is working. I think that command hold reference to item and GC doesn't free memory.

Is there any way to "unbind" command from item? I care about MVVM pattern.

I found this: https://atomaras.wordpress.com/2012/04/23/solving-mvvmlights-eventtocommand-memory-leak-wp7/ but it doesn't work. Is there any simpler solution?

FIXED

I fixed it by changing EventToCommand to InvokeCommandAction.


Solution

  • I fixed it by changing EventToCommand to InvokeCommandAction.

     <interactivity:Interaction.Triggers>
         <interactivity:EventTrigger EventName="Tap"> 
             <command:InvokeCommandAction Command="{Binding Mode=OneWay, Path=DataContext.NavigateToNextPage, Source={StaticResource Context}}" CommandParameter="{Binding}" />
         </interactivity:EventTrigger>
     </interactivity:Interaction.Triggers>