更新时间:2023-10-08 22:22:28
在MVVM模式中,View和ViewModel之间的理想沟通通过 Mediator ,以避免硬拷贝从VM查看。有一个调解员,
上面的CodeProject链接显示了如何实现一个调解器类。但是,我建议您使用流行的MVVM框架,因为您会发现它具有Mediator实现和许多其他可用于MVVM的工具开箱即用。
I'm working on some .Net XAML application using MVVM pattern. According to MVVM I keep my app logic in VM and in Code Behind I do UI-related actions. But I need to execute some UI-related code in Code Behind in respond to some logic in VM.
Example: I need to show an error message (custom toast notification in my case) when login operation failed. Login operation resides in VM, but I can't use any UI-specific classes in my VM, so I made an event in VM and hooking up to in in Code Behind, doing UI stuff.
Is it a violation of MVVM pattern? If yes, then how to solve my case?
Ideally communication between View and ViewModel in MVVM pattern done through Mediator to avoid hard-referencing View from VM. Having a mediator,
The CodeProject link above shows how to implement a mediator class. But I will suggest to use a popular MVVM framework since you'll find it has Mediator implementation and many other tools for MVVM available out-of-the-box.