ICommand CanExecuteChanged导致内存泄漏

时间:2013-10-14 21:59:54

标签: wpf

下面给出了抽象类及其实现.CanExecuteChanged导致泄漏。它对视图模型有很强的参考作用。我正在使用Ants profiler。它显然显示了处理程序的泄漏。

 public abstract class AlwaysCanExecuteCommand : ICommand
 {
  #region ICommand Members

  /// <summary>
  /// The CanExecute command always return true, so this command can always be executed
  /// </summary>
  /// <param name="parameter"></param>
  /// <returns>True; so command can always be executed</returns>
  public virtual bool CanExecute(object parameter)
  {
     return true;
  }

  /// <summary>
  /// Event that gets fired when this command's execution state changes
  /// </summary>
  public event EventHandler CanExecuteChanged;


  /// <summary>
  /// Protected virtual function to raise the CanExecuteChanged event
  /// </summary>
  /// <param name="e">Event argument</param>
  protected virtual void OnCanExecuteChanged(EventArgs e)
  {
     if (CanExecuteChanged != null)
        CanExecuteChanged(this, e);
  }

  /// <summary>
  /// Abstract Execute function, to always be overridden by derived classes to contain the
  /// code when command is executed
  /// </summary>
  /// <param name="parameter">Parameter</param>
  public abstract void Execute(object parameter);
  #endregion

}

 public class EditCompanyProfileCommand : AlwaysCanExecuteCommand
 {
  private Organization _organization;
  private bool _isCustomerView;
  private readonly CommonFunctionService _commonFunctionService;

  /// <summary>
  /// Initializes a new instance of the <see cref="EditCompanyProfileCommand"/> class.
  /// </summary>
  public EditCompanyProfileCommand(CommonFunctionService commonFunctionService)
  {
     _commonFunctionService = commonFunctionService;
  }

  /// <summary>
  /// Gets or Sets if this view is for a Customer
  /// </summary>
  public bool IsCustomerView
  {
     get { return _isCustomerView; }
     set { _isCustomerView = value; }
  }

  /// <summary>
  /// The current Organization
  /// </summary>
  public Organization Organization
  {
     get { return _organization; }
     set { _organization = value; }
  }

  public override void Execute(object parameter)
  {
     IList list = parameter as IList;

     if (list != null && list.Count > 0)
     {
        CompanyProfile cachedCompanyProfile = null;
        DataRowView dataRowView = list[0] as DataRowView;
        if (dataRowView != null)
        {
           Guid companyId = (Guid)dataRowView[dataRowView.Row.ItemArray.Length - 1];
           if (companyId != null)
              cachedCompanyProfile = CBC.Instance.GetFullyRealizedCompanyProfile(companyId);
        }
        else
           cachedCompanyProfile = list[0] as CompanyProfile;

        if (cachedCompanyProfile != null)
        {
          ....
        }
     }
  }

}

0 个答案:

没有答案