如何使用 caliburn.micro 绑定到命令属性?
我的行为公开了多个命令属性。通常,我使用 MVVM Light,只在视图模型中使用路由命令并绑定到这些命令。然而,这个项目正在使用 Caliburn.Micro,所以我尝试以 Caliburn.Micro 的方式进行操作。我可以在 Message.Attach 上使用一些语法来执行此操作(关于 Message.Attach 的解析器是否有任何好的文档)?
我是否必须修改公开事件的行为才能使其与 Caliburn.Micro 一起使用?
I have a behavior that exposes several command properties. Typically I use MVVM Light and would just use a routed command in my view model and bind to those. However, this project is using Caliburn.Micro so I'm trying to do it the Caliburn.Micro way. Is there some syntax I can use on the Message.Attach to do this (is there any good documentation on the parser for Message.Attach)?
Do I have to modify the behavior to expose events to make this work with Caliburn.Micro?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Caliburn.Micro 有自己的视图/视图模型通信机制,作为命令的替代方案,称为 Actions 。您应该能够将 Action 参数设置为现有命令的
Execute
委托,并且如果需要,设置适当的视图控件属性(例如IsEnabled
)以绑定到您的CanExecute
委托。Caliburn.Micro has its own mechanism for view/viewmodel communication as an alternative to commanding called Actions. You should be able to set the Action parameter to be your
Execute
delegate of your existing command, and if required set the appropriate view control property (e.g.IsEnabled
) to bind to yourCanExecute
delegate.只要您正确设置 View 和 ViewModel,Caliburn.Micro 就会为您处理路由 Actions(它使用一些隐式假设,这可能是也可能不是您喜欢的)以下是有关 Actions 的链接:http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions&referringTitle=Documentation
执行相同操作的更好、耦合性较低的方法是使用事件聚合器 - http://caliburnmicro.codeplex.com/wikipage?title=The%20Event %20Aggregator&referringTitle=Documentation
查看 Caliburn.Micro 中提供的 HelloEventAggregator 代码示例例如,来源...但基本要点是:
您创建自定义事件以供聚合器使用。
您的视图将发布这些自定义事件 - 它不关心谁在监听,只关心事件被发布。
您的 ViewModel 将被设置为通过添加 IHandle 来处理这些事件。
此方法通过允许视图真正只处理绑定到数据和发布事件,从而保持非常高的 SoC - 视图仍然不关心如何处理事件。
然后通过添加 IHandle 接口来设置每个视图模型来处理事件。 (请注意,单个 ViewModel 上可以有许多不同的 IHandle 接口)ViewModel 不关心事件是如何引发的,只关心它是如何引发的,并且它是处理来自聚合器的事件的权限。
Caliburn.Micro handles routing Actions for your, as long as you setup the View and ViewModel correctly (it uses some implicit assumptions, which may or may not be your cup of tea) Here is a link about Actions: http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions&referringTitle=Documentation
A better, less tightly coupled way, of doing the same thing is to use the Event Aggrigator - http://caliburnmicro.codeplex.com/wikipage?title=The%20Event%20Aggregator&referringTitle=Documentation
Take a look at the HelloEventAggrgator code sample available in the Caliburn.Micro source, for an example... But the basic jist is this:
You make custom events for use by the aggregator.
Your view will publish those custom events - it does not care who is listening, only that the event gets published.
Your ViewModels will be setup to handle those events, by adding IHandle
This method maintains very high SoC, by allowing the View to really only deal with binding to data, and publishing events - the view remains unconcerned about how the events are handled.
Each View Model is then setup to handle the Events by adding an IHandle interface. (Note that you can have many different IHandle interfaces on a single ViewModel) The ViewModel is unconcerned about how the event was raised, only that it was, and that it is the authority on handling that event from the Aggregator.
我最终将行为重写为触发器来处理这个问题。
I ended up rewriting the Behavior as a Trigger instead to handle this.