在PreviewExecuted之后执行未被调用

时间:2022-08-23 11:37:48

Here's my code:

这是我的代码:

var commandBinding = new CommandBinding(ApplicationCommand.New);
commandBinding.PreviewExecuted += OnPreviewExecuted;
commandBinding.Executed += OnExecuted;
CommandBindings.Add(commandBinding);

void OnPreviewExecuted(object sender, ExecutedRoutedEventArgs e) {
  e.Handled = false;
}

void OnExecuted(object sender, ExecutedRoutedEventArgs e) {
  DoSomething();
}

MSDN says:"...If the preview event is not handled, the Executed event is raised on command target."

MSDN说:“......如果未处理预览事件,则会在命令目标上引发Executed事件。”

This does work correctly for the PreviewCanExecute event. But in this case the Executed-Event will not get called when a PreviewExecuted-Event is listening.

这对PreviewCanExecute事件可以正常工作。但在这种情况下,当PreviewExecuted-Event正在侦听时,不会调用Executed-Event。

I did not find anything around this topic so I want to ask, whether the behavior is intended or just incorrect.

我没有找到关于这个主题的任何内容,所以我想问一下,这个行为是有意还是不正确。

1 个解决方案

#1


It seems it doesn't matter what you set e.Handled to.

似乎你设置e.Handled到什么并不重要。

Here is the code that decides which events to raise (ExecuteImpl in RoutedCommand.cs):

以下是决定要引发哪些事件的代码(RoutedCommand.cs中的ExecuteImpl):

ExecutedRoutedEventArgs args = new ExecutedRoutedEventArgs(this, parameter);
args.RoutedEvent = CommandManager.PreviewExecutedEvent;

if (targetUIElement != null)
{
    targetUIElement.RaiseEvent(args, userInitiated);
}
else
{
    ...
}

if (!args.Handled)
{
    args.RoutedEvent = CommandManager.ExecutedEvent;
    if (targetUIElement != null)
    {
        targetUIElement.RaiseEvent(args, userInitiated);
    }
    ...
}

So indeed, if e.Handled is false after the preview event, the Executed event should be raised. But it is never false after a PreviewExecuted handler is invoked (CommandBindings.cs, OnExecuted):

实际上,如果在预览事件之后e.Handled为false,则应该引发Executed事件。但是在调用PreviewExecuted处理程序后,它永远不会出错(CommandBindings.cs,OnExecuted):

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

It simply sets e.Handled to true after invoking the preview handler...

它只是在调用预览处理程序后将e.Handled设置为true ...

Why this is so, I have no idea. PreviewCanExecute works the same way, but it only sets e.Handled to true if e.CanExecute is set to true - if you do that in your preview handler, the CanExecute handler won't be called regardless of e.Handled.

为什么会这样,我不知道。 PreviewCanExecute以相同的方式工作,但如果e.CanExecute设置为true,它只将e.Handled设置为true - 如果在预览处理程序中执行此操作,则无论e.Handled如何都不会调用CanExecute处理程序。

My assumption is that "If the preview event is not handled" is an unfortunate wording of "If the preview event does not have a registered handler".

我的假设是“如果未处理预览事件”是“如果预览事件没有注册处理程序”的不幸措辞。

#1


It seems it doesn't matter what you set e.Handled to.

似乎你设置e.Handled到什么并不重要。

Here is the code that decides which events to raise (ExecuteImpl in RoutedCommand.cs):

以下是决定要引发哪些事件的代码(RoutedCommand.cs中的ExecuteImpl):

ExecutedRoutedEventArgs args = new ExecutedRoutedEventArgs(this, parameter);
args.RoutedEvent = CommandManager.PreviewExecutedEvent;

if (targetUIElement != null)
{
    targetUIElement.RaiseEvent(args, userInitiated);
}
else
{
    ...
}

if (!args.Handled)
{
    args.RoutedEvent = CommandManager.ExecutedEvent;
    if (targetUIElement != null)
    {
        targetUIElement.RaiseEvent(args, userInitiated);
    }
    ...
}

So indeed, if e.Handled is false after the preview event, the Executed event should be raised. But it is never false after a PreviewExecuted handler is invoked (CommandBindings.cs, OnExecuted):

实际上,如果在预览事件之后e.Handled为false,则应该引发Executed事件。但是在调用PreviewExecuted处理程序后,它永远不会出错(CommandBindings.cs,OnExecuted):

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

It simply sets e.Handled to true after invoking the preview handler...

它只是在调用预览处理程序后将e.Handled设置为true ...

Why this is so, I have no idea. PreviewCanExecute works the same way, but it only sets e.Handled to true if e.CanExecute is set to true - if you do that in your preview handler, the CanExecute handler won't be called regardless of e.Handled.

为什么会这样,我不知道。 PreviewCanExecute以相同的方式工作,但如果e.CanExecute设置为true,它只将e.Handled设置为true - 如果在预览处理程序中执行此操作,则无论e.Handled如何都不会调用CanExecute处理程序。

My assumption is that "If the preview event is not handled" is an unfortunate wording of "If the preview event does not have a registered handler".

我的假设是“如果未处理预览事件”是“如果预览事件没有注册处理程序”的不幸措辞。