C# 如何以编程方式发送命令

C# 如何以编程方式发送命令,c#,wpf,C#,Wpf,我的主窗口ViewModel中有以下ICommand RelayCommand busyIndicatorCommand; public ICommand BusyIndicatorCommand { get { if (busyIndicatorCommand == null) busyIndicatorCommand = new RelayCommand(BusyIndicatorCommandExecute, CanBusyIndi

我的主窗口ViewModel中有以下ICommand

RelayCommand busyIndicatorCommand;

public ICommand BusyIndicatorCommand
{
    get
    {
        if (busyIndicatorCommand == null)
            busyIndicatorCommand = new RelayCommand(BusyIndicatorCommandExecute, CanBusyIndicatorCommand);
        return busyIndicatorCommand;
    }
}
在XAML中,当您有一个按钮时,从子视图调用类似的内容是一个简单的过程

<Button Content="Press Me" 
        Command="{Binding DataContext.BusyIndicatorCommand, RelativeSource={RelativeSource AncestorType=Window}}"
        CommandParameter="{Binding EnableIndicator}"/>
在我的用户控件视图模型中添加了对它的引用,并像这样执行它

namespace UserControls
{
    class UserControlViewModel
    {

        public UserControlViewModel()
        {
            setBusyIndicator(true);
            Task.Factory.StartNew(() =>
            {
                //Do stuff here
            })
            .ContinueWith(t =>
            {
                Application.Current.Dispatcher.Invoke(new Action(() => setBusyIndicator(false)));
            });
        }

        private void setBusyIndicator(bool enable)
        {
            Libs_SharedCommands.Commands.cmd_BusyIndicator.Execute(enable, null);
        }

    }
}
最后将RoutedCommand处理程序添加到我的主窗口视图中,该视图反过来调用主窗口视图模型。对此相当满意,MVVM仍然完好无损

namespace Application
{
    public partial class MainView
    {
        public MainView()
        {
            InitializeComponent();

            CommandBinding cbBusyIndicator = new CommandBinding(Libs_SharedCommands.Commands.cmd_BusyIndicator, BusyIndicator_MainNavCmdExecute, MainNavCmdCanExecute);
            this.CommandBindings.Add(cbBusyIndicator);
        }

        private void BusyIndicator_MainNavCmdExecute(object sender, ExecutedRoutedEventArgs e)
        {
            setCommand(Libs_SharedCommands.Commands.CommandType.BusyIndicator, e.Parameter);
        }
        private void MainNavCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void setCommand(Libs_SharedCommands.Commands.CommandType view_type, object parameter)
        {
            var viewModel = (MainViewModel)DataContext;
            if (viewModel != null)
            {
                switch (view_type)
                {
                    case Libs_SharedCommands.Commands.CommandType.BusyIndicator:
                        viewModel.ShowBusyIndicator = Convert.ToBoolean(parameter);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
使用命令的以下命令:

BusyIndicatorCommand.Execute(EnableIndicator);

抱歉,应该说子视图/视图模型位于另一个项目中,并且已通过反射加载到主窗口中。因此,当使用按钮时,我猜测,因为我不知道它是如何工作的,但它使用视觉树来查找窗口,然后在windows视图模型中查找ICommand。因此,它不知道是否存在总线指示命令。所以我不能只使用BusyIndicatorCommand.Execute。请让我知道,如果这是任何不清楚的,我的术语可能是出来的@汉克:但是在你想调用命令的地方,是不是范围内有
BusyIndicatorCommand
的类型?如果是这样,您可以使用
as
操作符尝试将
DataContext
强制转换为该类型,如果成功,请访问
BusyIndicator命令
。BusyIndicator不在范围内。是否可以松散地执行某些操作?@Hank:您可以始终使用反射方法,然后再使用反射方法,例如检索
BusyIndicatorCommand
属性的值-前提是您可以以某种方式获取对具有该属性的对象的引用(即使其实际类型不在范围内)。我所说的松散是,不是直接调用BusyIndicatorCommand,而是创建一个事件或类似的东西,在视觉树上冒泡或隧道?我看过一些为按钮创建路由事件的教程:publicstatic readonly RoutedEvent SampleEvent=EventManager.RegisterRoutedEvent(“Tap”,RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(samplebuttonsinsimple));我可以实现这样的功能吗?
BusyIndicatorCommand.Execute(EnableIndicator);