使用“正常”指令

发布于 2024-10-01 10:28:53 字数 568 浏览 4 评论 0原文

我使用了MVVM Foundation的RelayCommand,现在,当我尝试使用“普通”ICommand时,我真的不知道如何使用它,特别是“绑定”它变量使用了我的主类。我只想创建一个在主类视图模型中引发事件 (RequestClose) 的 OkCommand 。另外,我只想在所有绑定都有效的情况下启用它。验证是使用 ValidationRule 实现的,如下所示

<TextBox>
    <TextBox.Text>
        <Binding Path="Blue">
            <Binding.ValidationRules>
                <validators:ByteValidator />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

I used RelayCommand of MVVM Foundation, now, when I try to use the "normal" ICommand, I don't really get how to use it, particularly "binding" it to variables used my my main class. I am just wanting to create a OkCommand that raises an event (RequestClose) in the main class, view model. Also, I want to enable it only if all bindings are valid. Validations are implemented using ValidationRule, something like below

<TextBox>
    <TextBox.Text>
        <Binding Path="Blue">
            <Binding.ValidationRules>
                <validators:ByteValidator />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

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

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

发布评论

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

评论(1

少钕鈤記 2024-10-08 10:28:54

ICommand 是一个接口,因此它本质上是抽象的。你不能直接使用它——你需要一些东西来实现它。 (例如RelayCommand

WPF 确实提供了ICommand 的内置实现:RoatedCommand。 (以及相关的派生类型,RoatedUICommand。)但是,这些类型是为非常特定的场景而设计的,与您所描述的不匹配。 (它们使用 UI 树的结构来计算出将处理命令的内容。这通常仅适用于对由控件直接提供的某些常见命令有多种不同实现的情况。例如,TextBox 知道如何实现剪切、复制、粘贴等,并且您希望具有焦点的 TextBox 来处理该命令。)

因此,实际上并没有“正常”的 ICommand 实现。而且,您可能不想使用验证规则...

验证规则非常有限,并且很难让它们支持您所描述的场景。它们有两个限制:

  1. 它们实际上只适用于完全独立的验证规则,无需任何上下文即可应用。 (例如“这个字符串是十进制数吗?”)
  2. 很难将它们与其他任何东西联系起来,因为整个机制没有上下文的概念。

第二个让你很难做你想做的事。这就是为什么它们没有被大量使用的原因,这大概就是为什么 Microsoft 引入了更好的机制的原因 - 对于大多数验证场景,您不使用 ValidationRule,而是让数据源实现 IDataErrorInfo。

原则是由您的数据源对象来决定它是否有效。然后,您的数据源对象还可以修改任何命令的有效性。这就是您如何安排在项目无效时禁用 OkCommand 对象。

由于 WPF 没有提供适合这种情况的 ICommand 的简单内置实现,这就是为什么人们觉得有必要编写像 RelayCommand 这样的东西。这是对 WPF 并没有真正内置的“正常”命令这一事实的回应 - 它只有时髦的路由命令。

ICommand is an interface, so it's inherently abstract. You can't use it directly - you need something that implements it. (E.g. RelayCommand)

WPF does provide a built in implementation of ICommand: RoutedCommand. (And a related derived type, RoutedUICommand.) However, these types are designed for a very specific scenario that doesn't match what you describe. (They use the structure of the UI tree to work out what will handle the command. This is usually only appropriate where you have multiple different implementations of some common command provided directly by controls. E.g., TextBox knows how to implement Cut, Copy, Paste, etc, and you want the TextBox that has the focus to handle the command.)

So there isn't really a "normal" ICommand implementation. And also, you probably don't want to use validation rules...

Validation rules are pretty limited, and it's very difficult to get them to support the scenario you describe. They have two limitations:

  1. They're really only good for completely self-contained validation rules that can be applied without needing any context. (E.g. "Is this string a decimal number?")
  2. It's hard to connect them with anything else, because the whole mechanism has no concept of context.

That second one makes it hard to do what you want. This is why they don't get used a whole lot, which is presumably why Microsoft introduced a better mechanism - for most validation scenarios, you don't use ValidationRule, and instead you make your data source implement IDataErrorInfo.

The principle is that it's your data source object that gets to decide whether it's valid or not. And your data source object can then also modify the validity of any commands. That's how you would arrange for the OkCommand object to be disabled when items are invalid.

And since WPF doesn't provide a simple built-in implementation of ICommand that suits this scenario, that's why people feel the need to write things like RelayCommand. It's a response to the fact that WPF doesn't really have a built-in "normal" command - it only has the funky routed commands.

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