有没有可以与 RoutedCommand 配合良好的 MVVM 框架?

发布于 2024-10-03 17:46:50 字数 1304 浏览 1 评论 0原文

RoutedCommand 和 RoutedUICommand 有很多用途。内置了对文本和键盘手势的支持。您可以将 RoutedUICommands 集合绑定到菜单的 ItemsSource,它将自动为您创建和绑定菜单项。开箱即用的控件自动支持某些命令(剪切、复制、粘贴)。

然而,每当我使用 MVVM 时,我都会发现路由命令不太适合,因为我必须为每个命令编写重复的样板 XAML 和代码隐藏。当我进行 MVVM 时,我通常会使用 RelayCommand 而不是路由命令。有时这是一个足够的解决方法,但它有缺点 - 例如,关键手势变得更加复杂< /a>.

但在观看 Rob Eisenberg 的“构建您自己的 MVVM 框架” 会议后,我不同意了对中继命令及其缺点不再满意。路由命令的唯一问题是重复的样板代码,一旦将重复代码分解到框架中并应用约定,似乎就不再有任何理由不在 MVVM 中使用路由命令。

从技术上来说,这看起来并不难。框架应该只需要绑定到视图的 CommandManager.CanExecute已执行< /a> 事件,并应用约定在 ViewModel 上查找方法和属性 - 例如,当执行 Cut 命令时,在 ViewModel 上查找 ExecuteCut 方法(和 CanExecuteCut 属性)。我可以构建自己的 MVVM 框架,让这一切变得简单。

但我的问题是,是否已有可与 RoutedCommand 配合使用的 MVVM 框架? 我唯一熟悉的 MVVM 框架是 Caliburn.Micro,目前它不支持这种方式的路由命令(这让我感到惊讶,因为它确实支持自己的动作冒泡风格)。

RoutedCommand and RoutedUICommand have a lot going for them. There's baked-in support for text and for keyboard gestures. You can bind a collection of RoutedUICommands to a Menu's ItemsSource and it will automatically create and bind menu items for you. Some commands (Cut, Copy, Paste) are automatically supported by out-of-the-box controls.

However, whenever I use MVVM, I've found routed commands to be a poor fit, because of the repetitive boilerplate XAML and codebehind I have to write for each one. When I'm doing MVVM, I've usually wound up using RelayCommand instead of routed commands. This is an adequate workaround some of the time, but it has downsides -- for example, key gestures become much more complicated.

But after watching Rob Eisenberg's "Build your own MVVM Framework" session, I'm no longer satisfied with relay commands and their shortcomings. The only problem with routed commands was the repetitive boilerplate code, and once you factor that repetitive code out into a framework, and apply conventions, there no longer seems to be any reason not to use routed commands in MVVM.

Technically, it looks like this would not be hard. The framework should just need to bind to the view's CommandManager.CanExecute and Executed events, and apply conventions to look for methods and properties on the ViewModel -- for example, when the Cut command is executed, look for an ExecuteCut method (and CanExecuteCut property) on the ViewModel. I could build my own MVVM framework that made this easy.

But my question is, are there already MVVM frameworks that work with RoutedCommand? The only MVVM framework I'm familiar with in any depth is Caliburn.Micro, and it doesn't currently support routed commands this way (which surprises me, since it does support its own flavor of action bubbling).

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

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

发布评论

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

评论(2

稳稳的幸福 2024-10-10 17:46:50

SO 和其他网站进行了大量的研究马拉松,没有找到任何使用 RoutedCommand 的正确框架。

但是 WPF 弟子 Josh Smith 已经实现了一个很好的解决方案,将路由命令与 MVVM 结合使用。

您可以检查 在 WPF 中使用带有 ViewModel 的 RoutedCommands,它被声明为 clean并且轻量级地在 MVVM 架构中使用 RoutedCommands。

with lot of research marathon in SO and other sites, didnt find any proper framework to PLAY with RoutedCommand.

but one of the WPF Disciple Josh Smith has implemented a nice solution of using Routed Command with MVVM.

you can Check Using RoutedCommands with a ViewModel in WPF, which is stated as clean and lightweight to use RoutedCommands in MVVM architecture.

故乡的云 2024-10-10 17:46:50

如果我没记错的话,Sacha Barber 在这个方向做了一些事情,我想你可以看看这里

If I remember correctly, Sacha Barber did something in that direction, I think you could have a look here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文