gpt4 book ai didi

c# - RoutedUICommand PreviewExecuted Bug?

转载 作者:太空狗 更新时间:2023-10-29 23:27:29 27 4
gpt4 key购买 nike

我正在使用 MVVM 设计模式构建应用程序,我想使用 ApplicationCommands 类中定义的 RoutedUICommands。由于 View(读取 UserControl)的 CommandBindings 属性不是 DependencyProperty,我们无法将 ViewModel 中定义的 CommandBindings 直接绑定(bind)到 View。我通过定义一个抽象的 View 类来解决这个问题,该类基于 ViewModel 接口(interface)以编程方式绑定(bind)它,该接口(interface)确保每个 ViewModel 都有一个 CommandBindings 的 ObservableCollection。这一切都很好,但是,在某些情况下,我想执行在不同类(View 和 ViewModel)中定义的相同命令的逻辑。例如,保存文档时。

在 ViewModel 中,代码将文档保存到磁盘:

private void InitializeCommands()
{
CommandBindings = new CommandBindingCollection();
ExecutedRoutedEventHandler executeSave = (sender, e) =>
{
document.Save(path);
IsModified = false;
};
CanExecuteRoutedEventHandler canSave = (sender, e) =>
{
e.CanExecute = IsModified;
};
CommandBinding save = new CommandBinding(ApplicationCommands.Save, executeSave, canSave);
CommandBindings.Add(save);
}

乍一看前面的代码就是我想要做的,但是文档绑定(bind)到的 View 中的 TextBox 仅在失去焦点时才更新其源代码。但是,我可以通过按 Ctrl+S 来保存文档而不会失去焦点。这意味着文档在源中更新的更改之前保存,有效地忽略了更改。但是,由于出于性能原因将 UpdateSourceTrigger 更改为 PropertyChanged 不是一个可行的选择,因此必须在保存之前执行其他一些强制更新。所以我想,让我们使用 PreviewExecuted 事件来强制更新 PreviewExecuted 事件,如下所示:

//Find the Save command and extend behavior if it is present
foreach (CommandBinding cb in CommandBindings)
{
if (cb.Command.Equals(ApplicationCommands.Save))
{
cb.PreviewExecuted += (sender, e) =>
{
if (IsModified)
{
BindingExpression be = rtb.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
}
e.Handled = false;
};
}
}

但是,为 PreviewExecuted 事件分配一个处理程序似乎完全取消了该事件,即使我明确地将 Handled 属性设置为 false 也是如此。所以我在前面的代码示例中定义的 executeSave 事件处理程序不再执行。请注意,当我将 cb.PreviewExecuted 更改为 cb.Executed 时,两段代码都执行,但顺序不正确。

我认为这是 .Net 中的一个错误,因为您应该能够将处理程序添加到 PreviewExecuted 和 Executed 并让它们按顺序执行,前提是您不将事件标记为已处理。

任何人都可以确认这种行为吗?还是我错了?这个错误有解决方法吗?

最佳答案

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.

如果您查看 CommandBinding 类源代码,就会发现 OnExecuted() 方法负责调用您通过 CommandBinding 为 PreviewExecuted 和 Executed 事件注册的处理程序。那里有一点:

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

这会将事件设置为在 PreviewExecuted 处理程序返回后立即处理,因此不会调用 Executed。

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.

请注意,只有在通过 CommandBinding 注册处理程序时,它才会以这种方式工作。

如果您仍然希望同时运行 PreviewExecuted 和 Executed,您有两个选择:

  1. 您可以从 PreviewExecuted 处理程序中调用路由命令的 Execute() 方法。想想看——在 PreviewExecuted 完成之前调用 Executed 处理程序时,您可能会遇到同步问题。在我看来,这不是一个好方法。
  2. 您可以通过 CommandManager.AddPreviewExecutedHandler() 静态方法单独注册 PreviewExecuted 处理程序。这将直接从 UIElement 类调用,不会涉及 CommandBinding。 编辑 2:查看帖子开头的第 4 点 - 这些是我们为其添加处理程序的事件。

从外观上看 - 这是故意这样做的。为什么?只能猜测...

关于c# - RoutedUICommand PreviewExecuted Bug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2281825/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com