首页 > 其他分享 >MVVM - Commands, RelayCommands and EventToCommand

MVVM - Commands, RelayCommands and EventToCommand

时间:2023-03-08 12:44:07浏览次数:45  
标签:Commands EventToCommand Windows Microsoft instance https RelayCommands com event

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


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

相关文章

  • Linux env commands
    1.新机新增root密码sudopasswdroot2.新增用户密码sudopasswd YOUR_USER_NAMENEWPW:NEWPW:3.SSHServersudoapt-getinstallopenssh-server SSHseting......
  • commands for use VIM in ubuntu
    Usevimxx.yytoopenafilefirstly.Theninputi,youcaninsertnewtextintheopenedfile.Whenyouwanttoendtheinput,youfirstlyclickEscbottom,t......
  • Commands and Arguments in Docker
    如果你按照下面的方式运行了一个ubuntu容器,你将会发现一段时间后,容器退出了,为什么会发生这种情况?这是因为,不像virtualmachine,containers无意于托管osoperationsystem,c......
  • Linux Must-Learnt Commands
    Clearsuccessfulloginhistory:echo>/var/log/wtmpViewloginhistory:lastClearcommand'sinputhistory:history-c#temperarllysudoaptinstall-ywipewipe~/.......
  • git commands Graphical Tutorial All In One
    gitcommandsDiagramTutorialAllInOnegif动画,图解gitcommandsAllInOnegitmergegitrebasegitresetgitrevertgitcherry-pickgitfetchgitpull......
  • Linux Must-Learnt Commands
    Clearsuccessfulloginhistory:echo>/var/log/wtmpViewloginhistory:lastClearcommand'sinputhistory:history-c......
  • Python: 执行系统命令的四种方法(os.system、os.popen、commands、subprocess)
    一、os.system方法这个方法是直接调用标准C的system()函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息。os.system(cmd)的返回值。如果执行成功,那么会......
  • (二)Redis命令学习之 StringCommands
    packageredis.clients.jedis.commands;importjava.util.List;importredis.clients.jedis.args.BitCountOption;importredis.clients.jedis.args.BitOP;importr......
  • (一)jedis 接口之 KeyCommands
    publicinterfaceKeyCommands{booleanexists(Stringvar1);longexists(String...var1);longpersist(Stringvar1);Stringtype(String......