windows phone 8 - 内存泄漏导致交互命令
windows phone 8 - memory leak causing interactivity commands
在 Windows Phone 8 应用程序中,我有 ItemsControl
和 ItemTemplate
有点击事件:
<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 不会释放内存。
有什么方法可以从项目中 "unbind" 命令吗?我关心 MVVM 模式。
我发现了这个:https://atomaras.wordpress.com/2012/04/23/solving-mvvmlights-eventtocommand-memory-leak-wp7/ 但它不起作用。有没有更简单的解决方案?
固定
我通过将 EventToCommand
更改为 InvokeCommandAction
来修复它。
我通过将 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>
在 Windows Phone 8 应用程序中,我有 ItemsControl
和 ItemTemplate
有点击事件:
<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 不会释放内存。
有什么方法可以从项目中 "unbind" 命令吗?我关心 MVVM 模式。
我发现了这个:https://atomaras.wordpress.com/2012/04/23/solving-mvvmlights-eventtocommand-memory-leak-wp7/ 但它不起作用。有没有更简单的解决方案?
固定
我通过将 EventToCommand
更改为 InvokeCommandAction
来修复它。
我通过将 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>