是否可以从MVVM中的View订阅ViewModel的.NET事件? [英] Is it OK to subscribe to the ViewModel's .NET events from the View in MVVM?

查看:86
本文介绍了是否可以从MVVM中的View订阅ViewModel的.NET事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVVM模式通过动画化包含2位数字的标签来编写主要内存训练器在屏幕上显示,并要求用户为每个数字快速键入相应的助记符.这完全取决于View动画的制作方式,因此后面将有一些代码.当在用户界面中键入了正确的助记符或数字消失时,将在视图中执行命令以中继发生这种情况.

I am writing this major memory trainer using the MVVM pattern by animating labels containing 2 digit numbers across the screen and asking the user to quickly type in the corresponding mnemonic for each number. It is entirely up to the View how the animation is done so there will be some code behind for this. When the correct mnemonic is typed into the UI or when the number disappears off the screen Commands will be executed from the View to relay that this has happened.

在ViewModel中,我想定期触发View为其动画的新数字(根据需要).

In the ViewModel I want to periodically fire off new numbers with which the View animates (as it pleases).

实现此目标的最佳方法是什么?我可以在ViewModel中有一个ObservableCollection<>,但我想做的不仅仅是绑定到它,我还需要在添加和删除数字时在后面的代码中执行一个方法.

What's the best way of achieving this? I can have an ObservableCollection<> in the ViewModel but I want to do more than simply bind to it, I will need to execute a method in the code behind when numbers are added and removed.

是否按照MVVM在ViewModel中使用简单的.NET事件并通过以下方式订阅它们:DataContext.NumberAdded + = new NumberAddedEventHandler(....)还是我应该这样做? >

Is it in accordance with MVVM to use simple .NET events in the ViewModel and subscribe to them with: DataContext.NumberAdded += new NumberAddedEventHandler(....) or is there another way I should be doing it?

推荐答案

视图是对ViewModel的一种用户友好的反映.如果您要运行特定于视图的逻辑(例如触发动画),则没有理由不使用后台代码来运行它.

A View is meant to be a user-friendly reflection of the ViewModel. If you have view-specific logic to run (such as triggering an animation), there's no reason not to use code-behind to run it.

只要您将UI和数据层分开,就可以了.

Providing you keep your UI and data layers separate, you're fine.

也就是说,如果仅从View层使用它,那么从ViewModel提供NumberAdded事件对我来说真的没有任何意义.这混合了您的图层.

That said, providing a NumberAdded event from the ViewModel doesn't really make sense to me if you're only using it from the View layer. That's mixing up your layers.

相反,我只会使用常规的CollectionChanged

Instead I would simply use the regular CollectionChanged

((MyViewModel)this.DataContext).Numbers.CollectionChanged += 
    new CollectionChangedEventHandler(....);

根据集合与UI的绑定方式,您还可以使用UI事件,也可以使用触发器.

Depending on how your collection is bound to the UI, you may also be able to use a UI event, or possibly triggers instead.

我认为带有ItemsSource的元素会在添加或删除项目时引发一个事件,或者您可以将ItemsSource属性强制转换为一个集合,然后在其中引用CollectionChanged事件,而无需引用MyViewModel

I thought elements with an ItemsSource raised an event when an item got added or removed, or you could simply cast the ItemsSource property into a collection and hook up to the CollectionChanged event there without needing to reference MyViewModel

void SomeItemsControl_DataContextChanged(...)
{
    var collection = (SomeItemsControl.ItemsSource as ObservableCollection);
    if (collection != null)
        collection.CollectionChanged += new CollectionChangedEventHandler(....);
}

这篇关于是否可以从MVVM中的View订阅ViewModel的.NET事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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