C# WPF中DelegateCommand的额外参数

C# WPF中DelegateCommand的额外参数,c#,wpf,delegates,prism,mef,C#,Wpf,Delegates,Prism,Mef,我已经实现了Microsoft.Practices.Prism.Commands.DelegateCommand,如下所示,以显示基于用户操作的视图,但我想向OneExecute方法传递一个额外的参数。我需要FilterInfo对象,它可以帮助我在加载视图时过滤数据。但我不确定如何将FilterInfo对象传递给OnExecute public class CustomCommand<T> : DelegateCommand<T>,ICommand { private s

我已经实现了Microsoft.Practices.Prism.Commands.DelegateCommand,如下所示,以显示基于用户操作的视图,但我想向
OneExecute
方法传递一个额外的参数。我需要
FilterInfo
对象,它可以帮助我在加载视图时过滤数据。但我不确定如何将
FilterInfo
对象传递给
OnExecute

public class CustomCommand<T> : DelegateCommand<T>,ICommand
{
private string _commandName;
public CustomCommand(string _commandName, Action<T> executeMethod) : base(executeMethod)
        {
            this._commandName = _commandName;
        }
}
public类CustomCommand:DelegateCommand、ICommand
{
私有字符串_commandName;
public CustomCommand(字符串_commandName,Action executeMethod):基本(executeMethod)
{
这个._commandName=_commandName;
}
}
我已将基本模块定义如下:-

public abstract class AbstractModuleBase : IModule
    {
      public List<CustomCommand> _customCommands = new List<CustomCommand>();

      protected void AddCustomCommand<TView>(string name, FilterInfo filterInfo=null)
        where TView : ViewModel
        {
            this._customCommands.Add(new CustomCommand<TView>(name, new Action<TView>(ModuleBase.OnExecute<TView>))
            {
                FilterInfo = filterInfo
            });
        }

        private static void OnExecute<TView>(TView obj)
        where TView : ViewModel
        {
          // Activate UI here.
          // Identified the view bashed on typeof(TView) and get it from MEF or IOC.
          // But here before calling the view i wanted to get FilterInfo obj.
        }
    }
公共抽象类AbstractModuleBase:IModule
{
public List_customCommands=new List();
受保护的void AddCustomCommand(字符串名称,FilterInfo FilterInfo=null)
where TView:ViewModel
{
此._customCommands.Add(新CustomCommand(名称,新操作(ModuleBase.OnExecute))
{
FilterInfo=FilterInfo
});
}
专用静态void OnExecute(TView obj)
where TView:ViewModel
{
//在这里激活用户界面。
//识别了typeof(TView)上的视图,并从MEF或IOC获取它。
//但在调用视图之前,我想获取FilterInfo obj。
}
}
我对xaml的定义如下

<ListBox ItemsSource="{Binding CustomCommandList}">
            <ListBox.ItemTemplate>
<DataTemplate >
               <Button Command="{Binding CustomCommand}"></Button>
</DataTemplate>     
</ListBox.ItemTemplate>
</ListBox>

上面的自定义命令与ViewModel绑定

[Export]
    public class DashViewModel
    {
        private ObservableCollection<CustomCommand> _CustomCommandList = AbstractModuleBase._customCommands;
    }
}   
[导出]
公共类视图模型
{
私有ObservableCollection\u CustomCommandList=AbstractModuleBase.\u customCommands;
}
}   
在应用程序启动时,我调用下面的方法以使用命令注册视图:-

AbstractModuleBase.AddCustomCommand<DashViewModel>("Dash Name", new FilterInfo("Some Filter"));
AbstractModuleBase.AddCustomCommand<EmpViewModel>("Emp Name", new FilterInfo("Some Filter"));
AbstractModuleBase.AddCustomCommand(“破折号名称”,新过滤器信息(“某些过滤器”);
AddCustomCommand(“Emp名称”,新过滤器信息(“某些过滤器”);

下面是我在评论中建议的一个例子

创建一个
ICommand
实现,将某些内容传递给参数,如下图所示:

public class FilterCommand : ICommand
{
    public FilterCommand( FilterInfo filterInfo )
    {
        _filterInfo = filterInfo;
    }

    #region ICommand
    public bool CanExecute( object parameter )
    {
        return true;
    }

    public void Execute( object parameter )
    {
        ((Action<FilterInfo>)parameter)( _filterInfo );
    }

    public event EventHandler CanExecuteChanged;
    #endregion

    #region private
    private readonly FilterInfo _filterInfo;
    #endregion
}
公共类筛选器命令:ICommand
{
公用筛选器命令(筛选器信息筛选器信息)
{
_filterInfo=filterInfo;
}
#区域ICommand
公共布尔CanExecute(对象参数)
{
返回true;
}
public void Execute(对象参数)
{
((动作)参数)(_filterInfo);
}
公共事件处理程序CanExecuteChanged;
#端区
#地区私人
私有只读筛选器信息\u筛选器信息;
#端区
}

通过这种方式,您可以得到一个命令,您可以将该命令作为参数传递给您的方法,以及一个该命令传递给您的方法的参数(作为构造函数参数)。

下面是我在注释中建议的示例

创建一个
ICommand
实现,将某些内容传递给参数,如下图所示:

public class FilterCommand : ICommand
{
    public FilterCommand( FilterInfo filterInfo )
    {
        _filterInfo = filterInfo;
    }

    #region ICommand
    public bool CanExecute( object parameter )
    {
        return true;
    }

    public void Execute( object parameter )
    {
        ((Action<FilterInfo>)parameter)( _filterInfo );
    }

    public event EventHandler CanExecuteChanged;
    #endregion

    #region private
    private readonly FilterInfo _filterInfo;
    #endregion
}
公共类筛选器命令:ICommand
{
公用筛选器命令(筛选器信息筛选器信息)
{
_filterInfo=filterInfo;
}
#区域ICommand
公共布尔CanExecute(对象参数)
{
返回true;
}
public void Execute(对象参数)
{
((动作)参数)(_filterInfo);
}
公共事件处理程序CanExecuteChanged;
#端区
#地区私人
私有只读筛选器信息\u筛选器信息;
#端区
}

通过这种方式,您可以获得一个可以传递给方法的命令(作为参数)和一个命令传递给方法的参数(作为构造函数参数)。

您希望实现什么?这个设置看起来有点奇怪。。。为什么自定义命令列表(静态)在
IModule
中?当然,当调用
Execute
时,您的
CustomCommand
可以将其
FilterInfo
传递给
executeMethod
。你不能从
DelegateCommand
派生,因为它的
Execute
不是虚拟的。我有一个使用MEF的基于模块和插件的应用程序,当模块或插件加载时,他们需要在自定义列表中添加查看命令。此列表数据正在仪表板上进行渲染以进行导航(绑定命令)。目前这是工作良好,但我想有自定义过滤器以及。我有非常大的设置,但为了在这里解释,我添加了重要的代码流。自定义命令列表与模块无关,我想应该单独管理。我甚至可以说,您的自定义命令列表不应该包含
ICommand
s,而应该包含传递给
ICommand
的参数。但是如果你想保留它,你必须提供你自己的实现。。。可能是复制了大量的
DelegateCommand
你能给我举个例子吗,这可以帮助我把你的想法联系起来。你想实现什么?这个设置看起来有点奇怪。。。为什么自定义命令列表(静态)在
IModule
中?当然,当调用
Execute
时,您的
CustomCommand
可以将其
FilterInfo
传递给
executeMethod
。你不能从
DelegateCommand
派生,因为它的
Execute
不是虚拟的。我有一个使用MEF的基于模块和插件的应用程序,当模块或插件加载时,他们需要在自定义列表中添加查看命令。此列表数据正在仪表板上进行渲染以进行导航(绑定命令)。目前这是工作良好,但我想有自定义过滤器以及。我有非常大的设置,但为了在这里解释,我添加了重要的代码流。自定义命令列表与模块无关,我想应该单独管理。我甚至可以说,您的自定义命令列表不应该包含
ICommand
s,而应该包含传递给
ICommand
的参数。但是如果你想保留它,你会