RoutedUICommand PreviewExecuted Bug?

前端 未结 2 448
刺人心
刺人心 2021-01-03 03:21

I\'m building an application using the MVVM design pattern and I want to make use of the RoutedUICommands defined in the ApplicationCommands class. Since the CommandBindings

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-03 04:06

    EDIT 2: From looking at the source code it seems that internally it works like that:

    1. The UIElement calls CommandManager.TranslateInput() in reaction to user input (mouse or keyboard).
    2. The CommandManager then goes through CommandBindings on different levels looking for a command associated with the input.
    3. When the command is found its CanExecute() method is called and if it returns true the Executed() is called.
    4. In case of RoutedCommand each of the methods does essencially the same thing - it raises a pair of attached events CommandManager.PreviewCanExecuteEvent and CommandManager.CanExecuteEvent (or PreviewExecutedEvent and ExecutedEvent) on the UIElement that initiated the process. That concludes the first phase.
    5. Now the UIElement has class handlers registered for those four events and these handlers simply call CommandManager.OnCanExecute() and CommandManager.CanExecute() (for both preview and actual events).
    6. It is only here in CommandManager.OnCanExecute() and CommandManager.OnExecute() methods where the handlers registered with CommandBinding are invoked. If there are none found the CommandManager transfers the event up to the UIElement's parent, and the new cycle begins until the command is handled or the root of the visual tree is reached.

    If you look at the CommandBinding class source code there is OnExecuted() method that is responsible for calling the handlers you register for PreviewExecuted and Executed events through CommandBinding. There is that bit there:

    PreviewExecuted(sender, e); 
    e.Handled = true;
    

    this sets the event as handled right after your PreviewExecuted handler returns and so the Executed is not called.

    EDIT 1: Looking at CanExecute & PreviewCanExecute events there is a key difference:

      PreviewCanExecute(sender, e); 
      if (e.CanExecute)
      { 
        e.Handled = true; 
      }
    

    setting Handled to true is conditional here and so it is the programmer who decides whether or not to proceed with CanExecute. Simply do not set the CanExecuteRoutedEventArgs's CanExecute to true in your PreviewCanExecute handler and the CanExecute handler will be called.

    As to ContinueRouting property of Preview event - when set to false it prevents the Preview event from further routing, but it does not affect the following main event in any way.

    Note, that it only works this way when handlers are registered through CommandBinding.

    If you still want to have both PreviewExecuted and Executed to run you have two options:

    1. You can can call Execute() method of the routed command from within PreviewExecuted handler. Just thinking about it - you might run into sync issues as you're calling Executed handler before the PreviewExecuted is finished. To me this doesn't look like a good way to go.
    2. You can register PreviewExecuted handler separately through CommandManager.AddPreviewExecutedHandler() static method. This will be called directly from UIElement class and will not involve CommandBinding. EDIT 2: Look at the point 4 at the beginning of the post - these are the events we're adding the handlers for.

    From the looks of it - it was done this way on purpose. Why? One can only guess...

提交回复
热议问题