2013-03-08 4 views
0

DelegateCommand 클래스가 있고 그 안에 2 개의 생성자가 있습니다. 내가 그 클래스의 생성자에 내 재산을 통과 할 때 나는라는 오류 메시지가 얻을 : 내 DelegateCommand를 들어DelegateCommand에 대한 오버로드 된 메소드

Error 1 The best overloaded method match for 'QMAC.ViewModels.DelegateCommand.DelegateCommand(System.Action<object>)' has some invalid arguments 

Error 2 Argument 1: cannot convert from 'System.Windows.Input.ICommand' to 'System.Action<object>' 

을, 여기에 내가 (코멘트 짧은 그것을 유지하지 않고) 무엇을 가지고 :

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows.Input; 

namespace QMAC.ViewModels 
{ 
    class DelegateCommand : ICommand 
    { 
     private Action<object> _execute; 
     private Predicate<object> _canExecute; 

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

     } 

     public DelegateCommand(Action<object> execute, Predicate<object> canExecute) 
     { 
      _execute = execute; 
      _canExecute = canExecute; 
     } 

     public bool CanExecute(object parameter) 
     { 
      if (_canExecute.Equals(null)) 
      { 
       return true; 
      } 

      return _canExecute(parameter); 
     } 

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

     public void RaiseCanExecuteChanged() 
     { 
      if (CanExecuteChanged != null) 
      { 
       CanExecuteChanged(this, EventArgs.Empty); 
      } 
     } 
    } 
} 

여기에 호텔과 내가 통과하려고하는 기능입니다 : 1 오류가 난 그냥 정말 간단 뭔가를 내려다 해요 알고 오버로드 된 방법으로 처리해야하기 때문에

using QMAC.Models; 
using System; 
using System.Collections.Generic; 
using System.Net.NetworkInformation; 
using System.Text; 
using System.Windows.Input; 
using System.Windows.Threading; 

namespace QMAC.ViewModels 
{ 
    class MainViewModel: ViewModelBase 
    { 
     Address address; 
     Location location; 
     private string _locationPicked; 
     private string _ipAddress; 
     private DelegateCommand _exportCommand; 

     public MainViewModel() 
     { 
      address = new Address(); 
      location = new Location(); 
      _ipAddress = address.IPAddress; 
      _exportCommand = new DelegateCommand(ExportCommand); 
     } 

public ICommand ExportCommand 
     { 
      get { return _exportCommand; } 
     } 

     public void ExportList() 
     { 

     } 
    } 
} 

은. 두 번째 오류에 관해서는, 내가 그 Property를 통과 할 수 없기 때문에 그것을 처리하는 더 좋은 방법은 무엇이겠습니까.

+1

생성자는'Action '을 취하고'ICommand'는 취하지 않습니다. 그리고 그럴지라도 설정하려는 필드를 참조하는 속성을 전달하고 싶지는 않습니다. – juharr

+0

그럼 어떻게해야합니까? – tylerbhughes

답변

3

DelegateCommand의 생성자가 을 가져 오도록 구성되어 있지 않고 대신 ICommand 인스턴스를 전달하고 있지 않습니다.

발생해야하는 작업으로 구성해야합니다. 예를 들어, 당신은 대신 "ExportList"전화를 의미 생각 :

_exportCommand = new DelegateCommand((o) => this.ExportList()); 

참고 : ExportList 실제로 당신이 전화를 걸 익명의 방법을 사용할 필요가 객체 매개 변수가 없기 때문에.

+0

비 λ 버전은 다음과 같을 것입니다 : _exportCommand = new DelegateCommand (ExportList()); – tylerbhughes

+0

@RandomlyKnighted - 아니, 왜냐하면 그 결과에 대한 함수를 호출하고 있기 때문입니다. 대개 함수에 델리게이트를 얻으려면 괄호없이 이름을 사용할 수 있지만이 경우'ExportList' 메소드는 필요한 메소드 서명과 일치하지 않습니다 (객체를 매개 변수로 사용하지 않습니다). 그래서 이것은 왜 람다 함수가 필요한지. – Reddog

관련 문제