如何从命令中获取事件功能[在MVVM中使用ICommand]

编程入门 行业动态 更新时间:2024-10-21 11:30:57
本文介绍了如何从命令中获取事件功能[在MVVM中使用ICommand]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们知道我们的UI元素有很多事件.每个都执行一些不同的功能. 如何通过命令获得这些功能. 例如,我们可以通过在按钮上应用命令绑定"来获得单击功能. 但是,如果我想对鼠标移动或鼠标进入或鼠标离开进行任何操作. 或者我想对数据网格的选择更改做一些事情.然后,如何在mvvm中使用Icommand来实现此功能.(因为我们无法在其中使用事件).

We know our UI elements have many events. Each doing some different functionality. How to get these functionalities with commands. For example we can have click functionality by applying "Command binding" on a button. But if i want to do something on mouse move or mouse enter or mouse leave on any element. Or i want to do something on selection change on a datagrid. Then how to achieve this functionality using Icommand in mvvm.(Because we can not use events there).

推荐答案

问题#1尚不清楚.它看起来不是问题,甚至不是正式的英语条款.看"But"之后写的是什么. 但是如果我想得到……"你想要它,那又是什么问题? 问题2: 您无需解释获取"参数的含义.如果声明并调用某个事件,则将一些值传递给事件Invoke的参数.添加事件处理程序时,需要编写处理程序的代码.这些参数将传递给处理程序代码,与调用事件时使用的参数相同.那么,问题出在哪里呢? 无论如何,这是完整的模式.首先,从声明一个偶数实例的类/结构开始: The question #1 is not clear. It does not look as a question or even a formally complete English clause. Look what is written after "But". "But if I want to get…" You want it, so what''s the question? Question #2: You don''t explain what do you mean by "get" parameters. If you declare and invoke some event, you pass some values to the parameters of event''s Invoke. When you add an event handler, you need to write the handler''s code. The parameters would be passed to the handler code, same parameters used when you invoke the events. So, where is the problem? Anyway, here is the complete pattern. First, start with the class/structure declaring a even instance: MyEventArgs = System.EventArgs { internal MyEventArgs(/*..*/) {/*..*/} //does not have to be public as you can only use it for invocation //usually, here you write properties to pass information on the event } class MyClass { System.EventHandler<MyEventArgs> SomethingHappening; System.EventHandler<MyEventArgs> SomethingHappened; private InvokeSomethingHappening() { if (SomethingHappening != null) SomethingHappening.Invoke( this, //usually new MyEventArgs(/*...*/)); //this is where the parameters are first created } //... }

调用调用添加到某个事件实例的调用列表中的所有事件处理程序(所有事件实例都可能是多播的).添加事件实例时,假定在调用中传递的参数将作为处理程序的实际参数传递.

Invocation calls all of the event handlers added to an invocation list of some event instance (all event instance are potentially multi-cast). When you add an event instance, you assume that the parameters passed in invocation will be passed as actual parameters of the handler.

MyClass myInsance = //... myInstance.SomethingHappening += delegate(object sender, MyEventArgs eventArgs) { //use sender, if needed (relatively rarely used) //use eventArgs (not used in simple events, used in more complex ones, such as mouse events) };

在这里,您不必获取"参数,而是在调用处理程序时传递它们. 注意:这只是建议的模式.严格来说,事件的委托类型不必具有基于发送者"和"eventArgs"模式的签名,但强烈建议使用该签名. 如果不清楚,请先阅读MSDN语言手册中的事件"一章:C#或使用的任何语言(C ++/CLI甚至VB.NET).这是最好的信息来源.如果仍然不清楚,欢迎您提出后续问题.

—SA

Here, you don''t "get" parameters, they are passed when a handler is called. Note: this is only the recommended pattern. Strictly speaking the event''s delegate type does not have to have the signature based on "sender" and "eventArgs" pattern, but using it is highly recommended. If this is not clear, first read the "Events" chapter in the language manual from MSDN: C# or whatever you use (C++/CLI or maybe even VB.NET). This is the best source of information. If this is still not clear, your follow-up questions are welcome.

—SA

更多推荐

如何从命令中获取事件功能[在MVVM中使用ICommand]

本文发布于:2023-11-02 14:38:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552674.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   事件   功能   ICommand   MVVM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!