gpt4 book ai didi

c# - DelegateCommand的重载方法

转载 作者:行者123 更新时间:2023-12-03 11:00:04 26 4
gpt4 key购买 nike

我有一个DelegateCommand类,其中有2个构造函数。当我将属性传递给该类的构造函数时,我收到一条错误消息,内容为:

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

对于我的DelegateCommand,这就是我所拥有的(不加任何评论以使其简短):
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);
}
}
}
}

这是我要传递的属性和功能:
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,因此是一种更好的处理方法。

最佳答案

您将DelegateCommand的构造函数配置为采用和Action<object>,而不是传递ICommand实例。

您应该使用应该采取的措施进行构建。例如,我认为您的意思是调用“ExportList”:

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

注意:由于ExportList实际上没有对象参数,因此您需要使用匿名方法进行调用。

关于c# - DelegateCommand的重载方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15285646/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com