【问题标题】:windows phone 8 - memory leak causing interactivity commandswindows phone 8 - 内存泄漏导致交互命令
【发布时间】:2015-12-23 09:00:09
【问题描述】:

在 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}" />

中继命令:

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

ItemsControl 已定义:

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

DepositsItems 是一个包含大约 200 个元素的列表,我有时会重新加载它。几次重新加载后,我有内存泄漏和应用程序关闭。我找到了它发生的原因。当我删除 Tap 事件时,一切正常。我认为该命令持有对项目的引用,并且 GC 不会释放内存。

有没有办法从项目中“解除绑定”命令?我关心 MVVM 模式。

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

已修复

我通过将EventToCommand 更改为InvokeCommandAction 来修复它。

【问题讨论】:

  • 请将您的修复作为答案发布
  • 接受它作为答案@darson1991

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    相关资源
    最近更新 更多