当 Button 完成 ICommand 的执行时,有没有办法获得回调?

发布于 2024-12-09 21:03:44 字数 64 浏览 0 评论 0原文

有没有办法可以在控件的按钮中附加处理程序(回调)(最终执行 ICommand),以便我的控件知道命令执行何时完成?

Is there a way I can attach a handler (callback) in buttons of my control (which eventually execute the ICommand) so that my control knows when a command execution is completed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-12-16 21:03:44

您可以创建抽象 CallbackableCommand 来引发回调方法。

abstract class CallbackableCommand : ICommand
  {
    private IInputElement getRaisedElement()
    {
      return Keyboard.FocusedElement;      
    }

    public void Execute(object parameter)
    {
      ExecuteImpl(parameter);
      var element = getRaisedElement();
      if(element == null) return;

      //var ci = typeof(ExecutedRoutedEventArgs).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
      //var routedEventArgs = (RoutedEventArgs)ci.Invoke(new object[] { this, parameter });
      var routedEventArgs = new RoutedEventArgs();
      //routedEventArgs.RoutedEvent = CommandManager.ExecutedEvent;
      routedEventArgs.RoutedEvent = Callbackable.CommandExecutedEvent;
      routedEventArgs.Source = element;
      routedEventArgs.Handled = false;

      element.RaiseEvent(routedEventArgs);            
    }    

    public abstract void ExecuteImpl(object parameter);

    abstract public bool CanExecute(object parameter);

    abstract public event EventHandler CanExecuteChanged;
  }

从 CallbackableCommand 继承您的命令并覆盖 CanExecute、CanExecuteChanged 和 ExecuteImpl(而不是 Execute)

  class SimpleCommand : CallbackableCommand
  {
    public override void ExecuteImpl(object parameter)
    {
      MessageBox.Show("Simple command execute with parameter: " 
        + parameter ?? "null");
    }

    public override bool CanExecute(object parameter)
    {
      return true;
    }

    public override event EventHandler CanExecuteChanged;
  }

自己的元素来指定 CommandExecuted 事件:

  public class Callbackable : ContentControl
  {
    public static readonly RoutedEvent CommandExecutedEvent = EventManager.RegisterRoutedEvent(
        "CommandExecuted", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Callbackable));

    // Provide CLR accessors for the event
    public event RoutedEventHandler CommandExecuted
    {
      add { AddHandler(CommandExecutedEvent, value); }
      remove { RemoveHandler(CommandExecutedEvent, value); }
    }
  }

编辑:
在您的控件中指定 Callbackable.CommandExecuted 事件

<Grid>
    <Grid.Resources>
        <local:SimpleCommand x:Key="btnCommand" />            
    </Grid.Resources>
    <local:Callbackable>
        <Button Command="{StaticResource btnCommand}"   
                CommandParameter="param"                
                local:Callbackable.CommandExecuted="Button_Executed" >
            Click me
        </Button>
    </local:Callbackable>
</Grid>

Executed 事件处理程序:

private void Button_Executed(object sender, ExecutedRoutedEventArgs e)
{
  MessageBox.Show("Executed");
}

You can create abstract CallbackableCommand that would raise callback method.

abstract class CallbackableCommand : ICommand
  {
    private IInputElement getRaisedElement()
    {
      return Keyboard.FocusedElement;      
    }

    public void Execute(object parameter)
    {
      ExecuteImpl(parameter);
      var element = getRaisedElement();
      if(element == null) return;

      //var ci = typeof(ExecutedRoutedEventArgs).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
      //var routedEventArgs = (RoutedEventArgs)ci.Invoke(new object[] { this, parameter });
      var routedEventArgs = new RoutedEventArgs();
      //routedEventArgs.RoutedEvent = CommandManager.ExecutedEvent;
      routedEventArgs.RoutedEvent = Callbackable.CommandExecutedEvent;
      routedEventArgs.Source = element;
      routedEventArgs.Handled = false;

      element.RaiseEvent(routedEventArgs);            
    }    

    public abstract void ExecuteImpl(object parameter);

    abstract public bool CanExecute(object parameter);

    abstract public event EventHandler CanExecuteChanged;
  }

Inherit your command from CallbackableCommand and override CanExecute, CanExecuteChanged and ExecuteImpl(instead of Execute)

  class SimpleCommand : CallbackableCommand
  {
    public override void ExecuteImpl(object parameter)
    {
      MessageBox.Show("Simple command execute with parameter: " 
        + parameter ?? "null");
    }

    public override bool CanExecute(object parameter)
    {
      return true;
    }

    public override event EventHandler CanExecuteChanged;
  }

Own element to specify CommandExecuted event:

  public class Callbackable : ContentControl
  {
    public static readonly RoutedEvent CommandExecutedEvent = EventManager.RegisterRoutedEvent(
        "CommandExecuted", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Callbackable));

    // Provide CLR accessors for the event
    public event RoutedEventHandler CommandExecuted
    {
      add { AddHandler(CommandExecutedEvent, value); }
      remove { RemoveHandler(CommandExecutedEvent, value); }
    }
  }

Edit:
In your control specify Callbackable.CommandExecuted event

<Grid>
    <Grid.Resources>
        <local:SimpleCommand x:Key="btnCommand" />            
    </Grid.Resources>
    <local:Callbackable>
        <Button Command="{StaticResource btnCommand}"   
                CommandParameter="param"                
                local:Callbackable.CommandExecuted="Button_Executed" >
            Click me
        </Button>
    </local:Callbackable>
</Grid>

Executed event handler:

private void Button_Executed(object sender, ExecutedRoutedEventArgs e)
{
  MessageBox.Show("Executed");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文