MVVM使用System.Windows.Interactivity和Microsoft.Expression.Interactions处理路由事件 [英] MVVM handle routed events with System.Windows.Interactivity and Microsoft.Expression.Interactions

查看:168
本文介绍了MVVM使用System.Windows.Interactivity和Microsoft.Expression.Interactions处理路由事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi


可以使用System.Windows.Interactivity和Microsoft.Expression.Interactions(ExpressionBlend SDK)来设置和处理由扳手符号表示的依赖项属性和路由事件Visual Studio 2012中的灯光符号?



我有一个窗口样本,当文本框被更改时(包含属性)包含文本框的方法MyMethod in class MyViewModel:调用INotifyPropertyChanged。这接近我想要的。



窗口中还有一个MediaElement,其中MediaOpened =mediaPlayer1_MediaEnded_1(路由事件)事件在后面的代码中处理:

class MainWindow:Window



我正在努力遵守MVVM风格。



xaml应该怎样和c#代码被修改,以便路由事件MediaOpened在类MyViewModel中处理?



请参阅http://database.hugetiger.com/dbExplore/MVVMCode.aspx

Hi
Can System.Windows.Interactivity and Microsoft.Expression.Interactions (ExpressionBlend SDK) be used to setup and handle both dependency properties and routed events denoted by the wrench symbol and the lighting symbol in Visual Studio 2012?

I have a obtained a sample of a window that contains a textbox when the textbox is changed (dependency property) a method MyMethod in class MyViewModel : INotifyPropertyChanged is invoked. This is close to what I want.

Also in the window is a MediaElement where the event MediaOpened="mediaPlayer1_MediaEnded_1" (routed event) is handled in the code behind:
class MainWindow : Window

I’m trying to comply with MVVM style.

How should the xaml and c# code be modified such that the routed event MediaOpened is handled in class MyViewModel?

See http://database.hugetiger.com/dbExplore/MVVMCode.aspx

推荐答案

您的问题是您无法从视图模型绑定到视图级事件。这打破了视图模型的无视觉依赖性规则。因此,您必须将路由事件转换为命令。



解决此问题的最常见方法是在MVVMLight工具箱中使用EventToCommand类。这是最简单的方法。但是,如果您不想使用MVVMLight,Blend库中也有类似的功能。它位于Interactivity命名空间中。此链接应该有所帮助: http://blogs.msdn.com/b/luc/archive/2010/11/18/executing-a-command-from-an-event-of-your-choice.aspx [< a href =http://blogs.msdn.com/b/luc/archive/2010/11/18/executing-a-command-from-an-event-of-your-choice.aspx\"target =_ blank title =新窗口> ^ ]



无论哪种方式,你都会得到相同的结果。
Your problem is that you can''t bind to a View-level event from a view-model. That breaks the no-visual-dependency rule for the view model. So you have to covert the routed event into a command.

The most common way to work around this is to use the EventToCommand class in the MVVMLight toolkit. It is the easiest way to do it. However, there is a similar function in the Blend libraries if you don''t want to use MVVMLight. It is in the Interactivity namespace. This link should help: http://blogs.msdn.com/b/luc/archive/2010/11/18/executing-a-command-from-an-event-of-your-choice.aspx[^]

Either way you will get the same result.


这篇关于MVVM使用System.Windows.Interactivity和Microsoft.Expression.Interactions处理路由事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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