MVVM - Commands, RelayCommands and EventToCommand
MVVM - Commands, RelayCommands and EventToCommand | Microsoft Learn
By Laurent Bugnion | May 2013
In previous installments of this series, I described the importance of decoupling the components of an application to make the application easier to unit test, maintain and extend. I also showed how adding design-time data makes it easier to work in Expression Blend or the Visual Studio designer in a visual manner.
In this article, I take a closer look at one of the important components of any Model-View-ViewModel application: the command. Historically, the .NET Framework has always been an event-based framework: a class exposes an event that is raised by the class instances when subscribers need to be notified. On the other hand, the subscribers provide an EventHandler, which is typically a method with two parameters: the sender of the event and an instance of a class deriving from EventArgs. When the event is raised, the event-handling method is executed and the EventArgs instance carries additional information (if available) about what caused the event in the first place.
This approach is fairly simple and successful for many scenarios. In .NET, it is often used to call back a subscriber after a Web operation completes (or fails). It is used by a sensor (such as location, orientation, proximity and so on) to notify the class that uses it that a condition has changed (for example, the user has moved, the screen has rotated, the device is close to another one, and the like). Most notably, this approach is used by UI elements to handle user events—for example, the click of a button, the movement of the mouse and many more.
For all their utility, event handlers have one problematic side effect: they can create a tight coupling between the instance that exposes the event and the instance that subscribes to it. The system needs to keep track of event handlers so that they can be executed when the event is raised, but the strong link this creates might prevent garbage collection. Of course, this isn’t an issue if the event handler is a static method, but it is not always possible to handle all events with static methods only. This is a frequent cause for memory leaks in .NET.
References
- The MVVM Light Toolkit can be downloaded at https://mvvmlight.codeplex.com.
- Bindable Application Bar for Windows Phone on Codeplex: https://bindableapplicationb.codeplex.com
- AppBarUtils for Windows Phone on Codeplex: https://appbarutils.codeplex.com
- “Introduction to Attached Behaviors in WPF” by Josh Smith: https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF
- “The Attached Behavior Pattern” by John Gossman: https://blogs.msdn.com/b/johngossman/archive/2008/05/07/the-attached-behavior-pattern.aspx
Laurent Bugnion is senior director for IdentityMine Inc., a Microsoft partner working with technologies such as Windows Presentation Foundation, Silverlight, Pixelsense, Kinect, Windows 8, Windows Phone and UX. He’s based in Zurich, Switzerland. He is also a Microsoft MVP and a Microsoft Regional Director.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-03-08 先使用大漩涡传送门,后召唤图腾的问题
2019-03-08 SxsTrace
2019-03-08 使用CCleaner卸载chrome
2018-03-08 Parallel in C#
2017-03-08 OneNote
2017-03-08 Find or Query Data with the mongo Shell
2016-03-08 socket listen backlog