我看到由事件处理程序引起的内存泄漏CanExecuteChanged
。
public class MyCommand : ICommand
{
#region ICommand Members
public virtual bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
protected virtual void OnCanExecuteChanged(EventArgs e)
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, e);
}
public void Execute(object parameter)
{
}
#endregion
}
当我运行 ANTS 内存分析器时,它显示CanExecuteChanged
绑定到按钮的事件处理程序effectivevalueentry
在我的视图模型中有一个。我的视图模型没有被垃圾收集。
XAML
<Button x:Uid="_editButton" x:Name="_editButton"
Tag="Edit"
Margin="1,1,1,1"
DockPanel.Dock="Left"
Command="{Binding Path=EditCommand}"
CommandParameter="{Binding Path=SelectedItems, ElementName=SearchResultGrid}"
Content="{DynamicResource icoEdit1512Image}"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Visibility="{Binding Path=NewEditDeleteButtonVisibility}"
Localization.Attributes="Tag (Text Modifiable)">
<Button.Style>
<Style x:Uid="Style_2" TargetType="{x:Type Button}"
BasedOn="{StaticResource dataGridToolBarButtonStyle}">
<Style.Triggers>
<DataTrigger x:Uid="DataTrigger_1" Binding="{Binding ElementName=SearchResultGrid,
Path=SelectedItem}"
Value="{x:Null}">
<Setter x:Uid="Setter_3" Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>