不使用 System.Windows.Input.ICommand 在 WPF 中实现 MVVM [英] Implementing MVVM in WPF without using System.Windows.Input.ICommand

查看:47
本文介绍了不使用 System.Windows.Input.ICommand 在 WPF 中实现 MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MVVM(模型-视图-视图模型)模式实现一个 WPF 应用程序,并且我希望将视图部分与模型和视图模型部分(一个 DLL)放在一个单独的程序集中(一个 EXE).

I'm trying to implement a WPF application using MVVM (Model-View-ViewModel) pattern and I'd like to have the View part in a separate assembly (an EXE) from the Model and ViewModel parts (a DLL).

这里的转折点是让 Model/ViewModel 程序集远离任何 WPF 依赖项.这样做的原因是我想从具有不同(非 WPF)UI 技术的可执行文件中重用它,例如 Mono 下的 WinForms 或 GTK#.

The twist here is to keep the Model/ViewModel assembly clear of any WPF dependency. The reason for this is I'd like to reuse it from executables with different (non-WPF) UI techs, for example WinForms or GTK# under Mono.

默认情况下,这是无法完成的,因为 ViewModel 公开了一个或多个 ICommand.但是ICommand类型定义在System.Windows.Input命名空间中,属于WPF!

By default, this can't be done, because ViewModel exposes one or more ICommands. But the ICommand type is defined in the System.Windows.Input namespace, which belongs to the WPF!

那么,有没有办法不使用ICommand来满足WPF绑定机制?

So, is there a way to satisfy the WPF binding mechanism without using ICommand?

谢谢!

推荐答案

您应该能够在 wpf 层中定义单个 WPF 自定义路由命令和单个命令处理程序类.您所有的 WPF 类都可以使用适当的参数绑定到这一命令.

You should be able to define a single WPF custom routed command in your wpf layer and a single command handler class. All your WPF classes can bind to this one command with appropriate parameters.

处理程序类然后可以将命令转换为您自己在 ViewModel 层中定义的自定义命令接口,并且独立于 WPF.

The handler class can then translate the command to your own custom command interface that you define yourself in your ViewModel layer and is independent of WPF.

最简单的例子是一个带有 Execute 方法的 void 委托的包装器.

The simplest example would be a wrapper to a void delegate with an Execute method.

所有不同的 GUI 层只需要在一个位置从它们的本机命令类型转换为您的自定义命令类型即可.

All you different GUI layers simply need to translate from their native command types to your custom command types in one location.

这篇关于不使用 System.Windows.Input.ICommand 在 WPF 中实现 MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆