2010-12-01 3 views
4

MVVM 패턴을 프로젝트에 적용하고 있습니다. ViewModel에 의해 노출 된 명령에 바인딩 된 단추가있는 UserControl 있습니다. 버튼이 보이기 때문에 버튼의 CanExecute 메소드를 계속 호출합니다. 뭔가가 성능 저하를 겪고 있다고 말하고 있지만 확실하지 않습니다. 이것은 예상 된 행동입니까? 또는 버튼을 명령에 묶는 더 좋은 방법이 있습니까?명령에서 연속 CanExecute 호출의 성능 저하

감사합니다.

+0

'CanExecute'를 (를) 호출하는 버튼이 계속 나타나는 이유는 무엇입니까? 기본적으로 그렇게하지 않으며 'ICommand.CanExecuteChanged'가 발생할 때만 그렇게해야합니다. – Jens

+0

어떤 유형의 ICommand를 사용하고 있습니까? CanExecute 업데이트와 관련하여 구현이 다르게 동작 할 수 있습니다. –

+0

난 그냥 일반 ICommand 인터페이스를 사용하고 있습니다. 실제로는 RelayCommand 클래스입니다.이 클래스는 델리게이트 주입을 사용하여 메서드를 지정한다는 점을 제외하고는 특별한 것이 아닙니다. –

답변

1

죄송합니다. 무슨 일이 일어나고 있는지 발견했습니다. 이것은 RelayCommand의 구현입니다.

public class RelayCommand : ICommand 
{ 
    #region Fields 

    readonly Action<object> _execute; 
    readonly Predicate<object> _canExecute; 

    #endregion // Fields 

    #region Constructors 

    public RelayCommand(Action<object> execute) 
     : this(execute, null) 
    { 
    } 

    public RelayCommand(Action<object> execute, Predicate<object> canExecute) 
    { 
     if (execute == null) 
      throw new ArgumentNullException("execute"); 

     _execute = execute; 
     _canExecute = canExecute; 
    } 
    #endregion // Constructors 

    #region ICommand Members 

    [DebuggerStepThrough] 
    public bool CanExecute(object parameter) 
    { 
     return _canExecute == null ? true : _canExecute(parameter); 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 

    public void Execute(object parameter) 
    { 
     _execute(parameter); 
    } 

    #endregion // ICommand Members 
} 

나는 시스템이 모든 명령을 자동으로 다시 쿼리하고 있다고 잘못 가정했다. 실제로는 각 명령의 CanExecuteChanged 이벤트에 연결되며 RelayCommand는 기본적으로 CanExecuteChanged 이벤트를 CommandManager의 RequerySuggested 이벤트에 연결하므로 시스템에서 재 쿼리를 "제안"할 때마다 실제로 모든 RelayCommands를 다시 쿼리합니다.

감사합니다.

+0

솔루션은 어디 있습니까? – qakmak

+0

무엇에? RequerySuggested 이벤트로 명령을 다시 쿼리하지 않으려면 RelayCommand를 사용하지 마십시오. –

+0

성능 문제를 해결했습니다. 나는 너를 고치려고 생각했다. ...... – qakmak