2009-05-22 3 views
0

Josh Smith 당 MVVM 패턴을 적용하는 데 어려움이 있습니다. 나는 여기서 문제를 연구 해왔고 문법이 옳은 것처럼 보이지 않는다.RelayCommand 람다 구문 문제

아래 코드는 필요한 구문을 따르는 것처럼 보이지만 Visual Studio에서 오류가보고됩니다. "위임 'System.Action'이 '2'인수를 사용하지 않습니다." "이 지정된 줄에 있습니다. 내가 실수를하고 어디

은 사람이 볼 수 있을까요? 감사!
+ 톰

RelayCommand _relayCommand_MoveUp; 
    public ICommand RelayCommand_MoveUp 
    { 
     get 
     { 
     if (_relayCommand_MoveUp == null) 
     { 
      _relayCommand_MoveUp = new RelayCommand(
      (sender, e) => this.Execute_MoveUp(sender, e),  **ERROR REPORTED HERE** 
      (sender, e) => this.CanExecute_MoveUp(sender, e)); 
      return _relayCommand_MoveUp; 
     } 
     } 
    } 

    private void Execute_MoveUp(object sender, ExecutedRoutedEventArgs e) 
    { 
     if (_selectedFolder != null) 
     { 
     _selectedFolder.SelectParent(); 
     } 
    } 

    private void CanExecute_MoveUp(object sender, CanExecuteRoutedEventArgs e) 
    { 
     e.CanExecute = (_selectedFolder != null) && (_selectedFolder.Parent != null); 
     } 


//And from Josh Smith: 

    public class RelayCommand : ICommand 
    { 
    public RelayCommand(Action<object> execute); 
    public RelayCommand(Action<object> execute, Predicate<object> canExecute); 

    public event EventHandler CanExecuteChanged; 

    [DebuggerStepThrough] 
    public bool CanExecute(object parameter); 
    public void Execute(object parameter); 
    } 

답변

2

RelayCommand 난 당신이 혼란 끝나는 곳이다 생각 RoutedCommand 아니다. 릴레이 명령에 대한

생성자는 Action delegate 및 옵션 Predicate delegate 걸릴. 이러한 대리자는 EventArgs를 사용하지 않고 단일 Object 매개 변수 만 사용하므로 오류가 발생합니다. 술어에는 bool의 리턴 유형도 필요합니다. 다음 오류는 다음과 같습니다. RouteCommand와 마찬가지로 e.CanExecute를 설정하는 대신 CanExecute 술어에서 true/false를 반환하면됩니다. 여기

는 보일 것입니다 방법은 다음과 같습니다 (주석에 대한 논의에서 추가)

public ICommand RelayCommand_MoveUp 
{ 
    get 
    { 
    if (_relayCommand_MoveUp == null) 
    { 
     _relayCommand_MoveUp = new RelayCommand(Execute_MoveUp, CanExecute_MoveUp); 

    } 
    return _relayCommand_MoveUp; 
    } 
} 

private void Execute_MoveUp(object sender) 
{ 
    if (_selectedFolder != null) 
    { 
    _selectedFolder.SelectParent(); 
    } 
} 

private void CanExecute_MoveUp(object sender) 
{ 
    return (_selectedFolder != null) && (_selectedFolder.Parent != null); 
} 



편집 :

당신이 할 것이다는 RoutedCommands처럼 뭔가 더를 사용하려면 ViewModels는 WPF 특정 뷰에 더 의존적이며, 사용할 수있는 몇 가지 좋은 옵션이 있습니다. 이 discussion

은 MVVM 시작과 함께 RoutedCommands를 사용하여 전체 아이디어를 얻었다.

그리고 here's 조쉬 스미스와 빌 KEMPF가 제시 한 문제에 매우 견고한 솔루션입니다.

+1

감사합니다, rmoore :

대리자에 매개 변수를 전달하려면, 당신은 그의 새로운 RelayCommand < T 대신 > 생성자를 사용해야합니다. 따라서 호출 된 메서드 내에서 EventArgs에 액세스해야 할 경우 RelayCommand 클래스를 사용하여 호출 할 수 없다는 점을 올바르게 이해합니까? + tom –

+0

맞습니다. 기본 ICommand는 어떤 이벤트도 구현하지 않습니다. 사실 내 지식에는 RoutedCommand가 없으며, 실제로 RoutedCommand가 찾는 CommandBinding이라는 것이 있습니다. 나는 많은) =이 댓글에 충분한 공간이 아니다으로, 내 게시물에 MVVM에 – rmoore

+0

감사 RoutedCommands에 대한 몇 가지 addtional 정보를 추가! +10 ... –

3

조쉬 스미스는 RelayCommand이 매개 변수를 대표 작동 방식을 변경 그의 MvvmFoundation 프로젝트에 코드 플렉스에 새로운 변화를 확인 이번 주말 (8 월 (22)). 조심해!

public ICommand GotoRegionCommand 
    { 
     get 
     { 
      if (_gotoRegionCommand == null) 
       _gotoRegionCommand = new RelayCommand<String>(GotoRegionCommandWithParameter); 
      return _gotoRegionCommand; 
     } 
    } 
    private void GotoRegionCommandWithParameter(object param) 
    { 
     var str = param as string; 
    } 
+0

정보를 제공해 주셔서 감사합니다. jasonD. 나는 새로운 MS MVVM Toolkit 0.1 Visual Studio 프로젝트 템플릿에 찬성하여 조쉬 스미스 코드를 포기했다. 그것은 사용하기 쉽고 아주 좋은 문서를 가지고 있습니다. http://wpf.codeplex.com/Wiki/View.aspx?title=WPF%20Model-View-ViewModel%20Toolkit을 참조하십시오. –