简单事件的处理MVVM [英] Simple Event Handling in MVVM

查看:136
本文介绍了简单事件的处理MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道是什么人了的想法就如何最好地处理在View从控制在一个视图模型的事件......以最轻量级的方式。

Just wondering what people had for ideas on how best to handle events in a ViewModel from controls on a View ... in the most lightweight way possible.

例如:

<MediaElement
     MediaOpened={Binding SomeEventHandler} />

在这种情况下,我们要处理的视图模型的MediaOpened事件。如果没有一个像棱镜的框架,人们将如何绑定这一个ViewModel?

In this case we want to handle the MediaOpened event in a ViewModel. Without a framework like Prism, how would one bind this to a ViewModel?

推荐答案

指挥 - 你SomeEventHandler需要一个类实现的ICommand ...有文学可用堆在线...

Commanding - your 'SomeEventHandler' needs to be a class that implements ICommand... there's a heap of literature available online...

此外 - 我会考虑让一个免费的,轻量级的'小'MVVM框架,比如 MvvmFoundation ,它提供在RelayCommand为这样一个目的(不学习PRISM的复杂性/开销)

Also - I would consider getting a free, lightweight 'mini' MVVM framework, such as MvvmFoundation, which provides the RelayCommand for just such a purpose (without the complexity/overhead of learning PRISM)

编辑:

看一看<一个href=\"http://geekswithblogs.net/HouseOfBilz/archive/2009/08/27/adventures-in-mvvm-ndash-binding-commands-to-any-event.aspx\">this博客用于连接命令无论如何...这是令人难以置信的强大,正如我所说的,但我猜你需要做一个判断调用,如果这是你想要的,喜欢的东西附加一个老式的事件,以及使用比较在后面,你的code超薄的机身事件处理程序只是调用了一些方法,在你的视图模型,是这样的:

Have a look at this blog for attaching command to any event... It is incredibly powerful, as I mentioned, but I guess you do need to make a judgement call if this is what you want, compared with something like attaching an old-fashioned event, and using a super-slim event handler in your code behind that simply invokes some method on your ViewModel, something like:

public void SomeEventHandler(object sender, SomeEventArgs e)
{
    MyViewModel vm = (MyViewModel)this.DataContext;
    vm.HandleLoadEvent( );
}

务实VS最佳实践...我跟你走的。)

pragmatic vs Best-practise... I'll leave it with you ;)

这篇关于简单事件的处理MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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