简体   繁体   English

分离视图,命令演示(文本,图标)和命令逻辑(执行,CanExecute)

[英]Separating views, command presentation (Text, Icon) and command logic (Execute, CanExecute)

If TL;DR: see the last paragraph. 如果TL; DR:见最后一段。

Pure WPF "suggests" putting presentation (controls, text, icons) into views and command logic (Execute, CanExecute methods) into code-behind. Pure WPF“建议”将表示(控件,文本,图标)放入视图和命令逻辑(Execute,CanExecute方法)到代码隐藏中。 Besides putting logic both into views ( CommandBindings ) and code-behind being a frowned upon practice, it doesn't help at all with XAML duplication : text, icons, large icons, hints, and numerous other properties have to be duplicated every time a command is used: for main menu, for context menu, for toolbar button, for ribbon button and other controls. 除了将逻辑放入视图( CommandBindings )和代码隐藏是一个不受欢迎的实践之外,它对XAML重复没有任何帮助:文本,图标,大图标,提示和许多其他属性必须每次复制使用命令:用于主菜单,用于上下文菜单,用于工具栏按钮,用于功能区按钮和其他控件。

Looks like the first problem (truly separating views and logic) is solved by DelegateCommand , RelayCommand and approaches like that. 看起来第一个问题(真正分离视图和逻辑)是通过DelegateCommandRelayCommand和类似方法解决的。 Command logic is moved into ViewModels (or Controllers in case of MVVMC), code-behind is clean, no CommandBindings and other nonsense in views. 命令逻辑被移动到ViewModels(或MVVMC中的控制器),代码隐藏是干净的,视图中没有CommandBindings和其他废话。

However, I can't find a commonly accepted solution to the presentation duplication problem. 但是,我找不到一个普遍接受的演示文稿重复问题的解决方案。 I want to separate command presentation (text, icons) and command logic ( Execute , CanExecute methods). 我想分离命令演示 (文本,图标)和命令逻辑ExecuteCanExecute方法)。 All code I could find either puts presentation into code (by creating a RoutedCommand with additional properties like Label and Icon ), or puts code into presentation (that is, handlers into views and code-behind). 我可以找到的所有代码都将表示放入代码中(通过创建带有LabelIcon等附加属性的RoutedCommand ),或者将代码放入表示中(即处理程序进入视图和代码隐藏)。 I don't like either. 我也不喜欢。 I think presentation should be completely in XAML, and code should be completely in CS (either in ViewModel or Controller). 我认为演示应该完全在XAML中,代码应该完全在CS中(在ViewModel或Controller中)。

Question: how to separate views (XAML with controls which reference commands), presentation of commands (labels, icons etc. for every command) and logic of commands (C# code for Execute , CanExecute etc. in ViewModels or Controllers)? 问题:如何分离视图(XAML与引用命令的控件),命令的显示(每个命令的标签,图标等)和命令逻辑(ViewModel或Controllers中的ExecuteCanExecute等的C#代码)?

There is no built-in solution to this problem, your're going to have to roll-up your sleeves and create the required structure yourself. 这个问题没有内置的解决方案,你必须自己卷起袖子并自己创建所需的结构。

In a recent project I worked on, I did exactly this. 在我最近的一个项目中,我做到了这一点。 I created a concept called an 'action' which supplements the WPF ICommand with other visual properties. 我创建了一个名为'action'的概念,它补充了WPF ICommand和其他视觉属性。 It was something like this ... 这是这样的......

interface IAction
{
  ICommand Command { get; }
  string DisplayText { get; }
  string ToolTipText{ get; }
  URI Icon { get; }
}

The application contained a collection of Action instances. 该应用程序包含一组Action实例。 These could then be bound to menus, toolbars etc ... allowing the same Action instance to be re-used with various different presentation styles. 然后可以将它们绑定到菜单,工具栏等......允许相同的Action实例重复使用各种不同的呈现样式。 It is all fairly straightforward MVVM stuff! 这一切都是相当直接的MVVM东西!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM