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.
标签:Commands,EventToCommand,Windows,Microsoft,instance,https,RelayCommands,com,event From: https://www.cnblogs.com/chucklu/p/17191640.html