0

I have a DelegateCommand class and within it 2 constructors. When I pass my property to the constructor of that class I get an error message that says:

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>'

For my DelegateCommand, here is what I have (without comments to keep it short):

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);
            }
        }
    }
}

Here is the property and the function that I am trying to pass:

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()
        {

        }
    }
}

Because the 1st error has to deal with an overloaded method I know I'm just overlooking something really simple. As for the second error, what would be a better way to handle that since I can't pass that Property.

Pallas
  • 1,499
  • 5
  • 25
  • 57
  • 1
    The constructor takes an `Action`, not an `ICommand`. And even if it did you wouldn't want to pass the property that refers to the field you are trying to set. – juharr Mar 08 '13 at 03:01
  • Then what should I pass to it? – Pallas Mar 08 '13 at 04:04

1 Answers1

3

Your constructor for the DelegateCommand is configured to take and Action<object> not you are instead passing an ICommand instance.

You should be constructing with the action that should be taking place. For example, I think you mean to call "ExportList" instead:

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

Note: because ExportList doesn't actually have an object parameter you need to use an anonymous method to make the call.

Reddog
  • 15,219
  • 3
  • 51
  • 63
  • Would the non-lambda version be: _exportCommand = new DelegateCommand(ExportList()); – Pallas Mar 08 '13 at 20:14
  • @RandomlyKnighted - No, because that's calling the function for it's result. Ordinarily to get the delegate to a function, you can use it's name without the parentheses but in this case, the `ExportList` method doesn't match the required method signature (it doesn't take an object as a parameter) so this is why the lambda function is required. – Reddog Mar 10 '13 at 22:04