接口实现在函数中有一个额外的参数

bas*_*rat 4 .net c# wpf

以下是ICommand成员的定义:http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.execute.aspx

签名是:

 
void Execute(
    Object parameter
)

它由RoutedCommand实现,具有以下签名(http://msdn.microsoft.com/en-us/library/system.windows.input.routedcommand.execute.aspx):


public void Execute(
    Object parameter,
    IInputElement target
)

RoutedCommand如何在成员函数中使用额外参数(IInputElement)实现ICommand?

Luk*_*keH 9

它使用显式接口实现来"隐藏" ICommand.Execute采用单个参数的方法.在Execute接受两个参数的方法不是的实现ICommand.Execute.

public class RoutedCommand : ICommand
{
    public void Execute(object parameter, IInputElement target)
    {
        // ...
    }

    // explicit interface implementation of ICommand.Execute
    void ICommand.Execute(object parameter)
    {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)