2010-01-26 3 views
1

WPF의 MVVM 패턴에 대해 알아 보려면 다음 tutorial을 수행하고 있습니다. ICommand 인터페이스를 구현 한 것으로 보이는 부분에 대해서는 내가 이해하지 못하는 부분이 있습니다.ICommand 구현 질문

아래 코드에서 _canExecute 변수는 메서드 및 변수로 사용됩니다. 일종의 이벤트라고 생각했지만 ICommand에는 이미 구현할 이벤트가 있으며 _canExecute가 아닙니다.

그래서 _canExecute가 무엇이되어야하는지에 관해 누군가 나를 도울 수 있습니까?

1: #region ICommand Members 
    2: 
    3: public bool CanExecute(object parameter) { 
    4:  return _canExecute == null ? true : _canExecute(parameter); 
    5: } 
    6: 
    7: public event EventHandler CanExecuteChanged { 
    8:  add { CommandManager.RequerySuggested += value; } 
    9:  remove { CommandManager.RequerySuggested -= value; } 
10: } 
11: 
12: public void Execute(object parameter) { 
13:  _execute(parameter); 
14: } 
15: 
16: #endregion 

답변

3

_canExecutePredicate<object> 일 때, _ executeAction<object>이됩니다.

다른 예는 내 delegate command blog post을 참조하십시오.

+0

켄트 (Kent) - 큰 문제는 아니지만 다음 사항을 참고하시기 바랍니다. http://meta.stackexchange.com/questions/5029 –

0

내가 잘못 될 수 있지만 지금까지 ICommand의 보통 구현 내가 자습서를 이해할 수있는 방법을 내가 아는 한, 버그가하고

public bool CanExecute(object parameter) { 
    return _execute == null ? false : true; 
} 

혹은 _canExecute가있다 읽어야 할 수있다 요청이 전달되는 함수 객체. 이 경우 자습서가 명확하지 않습니다.

어쨌든, 나는 그가 생각한 것을 저자와상의 할 것입니다.

0

나는 코드의 작가가 자료 Command 클래스의 상속인이

class DefaultCommand:BaseCommand 
{ 
    //_canExecute is supposed protected Predicate<string> in base class 
    public DefaultCommand() 
    { 
     base._canExecute =x=>x=="SomeExecutable"; 
    } 
} 
0

기본 가정처럼 처리 여부 또는하지 CanExecute를 결정할 수 있도록 CanExecute 항상하지 않는 사실이다 술어 위임을 통해 CanExecute 로직을 분리하고 싶었 생각 bool 대리인이 다른 평가를 위해 통과되었습니다.

+0

_canExecute의 반환 유형은 무엇입니까? 그게 내가 알아 내려고하는거야? –

+0

부울 (술어 형식) - 위의 구현에 따라야합니다. 대표자가됩니다. bool CanExecuteMethod (개체 매개 변수) { return ((Class) 매개 변수) .somethingReturningABoolean; } –