windows phone 8 - 内存泄漏导致交互命令

时间:2015-12-23 09:00:09

标签: c# windows-phone-8 mvvm-light unbind relaycommand

在Windows Phone 8应用程序中,我ItemsControlItemTemplate有点活动:

 <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>-->

上下文:

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

RelayCommand:

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

ItemsControl定义:

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

DepositsItems是一个包含大约200个元素的List,有时我会重新加载它。重新加载后,我有内存泄漏,应用程序关闭。我发现了它为什么会发生。当我删除Tap事件时,一切正常。我认为该命令持有对item和GC的引用并不释放内存。

有没有办法解开&#34;解开&#34;从项目命令?我关心MVVM模式。

我发现了这个:https://atomaras.wordpress.com/2012/04/23/solving-mvvmlights-eventtocommand-memory-leak-wp7/但它不起作用。有没有更简单的解决方案?

固定

我通过将EventToCommand更改为InvokeCommandAction来修复此问题。

1 个答案:

答案 0 :(得分:1)

我通过将 <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> 更改为go generate

来修复此问题
$ echo ${${X:-4711}:u}
4711
$ X=hello
$ echo ${${X:-4711}:u}
HELLO