在C#编码的事件情况下,前缀“On”的实现是什么?

在C#编码的事件情况下,前缀“On”的实现是什么?,第1张

概述我认为使用“On”作为C#方法的前缀存在相当大的困惑. 在MSDN文章“处理和提升事件”https://msdn.microsoft.com/en-us/library/edzehd2t(v=vs.110).aspx中,它说, Typically, to raise an event, you add a method that is marked as protected and virtual 我认为使用“On”作为C#方法的前缀存在相当大的困惑.

在MSDN文章“处理和提升事件”https://msdn.microsoft.com/en-us/library/edzehd2t(v=vs.110).aspx中,它说,

Typically,to raise an event,you add a method that is marked as
protected and virtual (in C#) or Protected and OverrIDable (in Visual
Basic). name this method OnEventname; for example,OnDataReceived. The
method should take one parameter that specifIEs an event data object.
You provIDe this method to enable derived classes to overrIDe the
logic for raising the event. A derived class should always call the
OnEventname method of the base class to ensure that registered
delegates receive the event.

指示On …方法是引发事件.但是,在许多编码样本中,甚至是Microsoft提供的一些代码样本中,我们可以看到事件On方法用作事件处理程序,就像在这里https://msdn.microsoft.com/en-us/windows/uwp/gaming/tutorial–adding-move-look-controls-to-your-directx-game?f=255&MSPPError=-2147217396

First,let’s populate the mouse and touch pointer event handlers. In
the first event handler,OnPointerpressed(),we get the x-y
coordinates of the pointer from the CoreWindow that manages our
display when the user clicks the mouse or touches the screen in the
look controller region.

voID MoveLookController::OnPointerpressed(_In_ CoreWindow^ sender,_In_ PointerEventArgs^ args){    // Get the current pointer position.    uint32 pointerID = args->CurrentPoint->PointerID;    DirectX::XMfloat2 position = DirectX::XMfloat2( args->CurrentPoint->position.X,args->CurrentPoint->position.Y );    auto device = args->CurrentPoint->PointerDevice;    auto deviceType = device->PointerDeviceType;    if ( deviceType == PointerDeviceType::Mouse )    {        // Action,Jump,or Fire    }    // Check  if this pointer is in the move control.    // Change the values  to percentages of the preferred screen resolution.    // You can set the x value to <preferred resolution> * <percentage of wIDth>    // for example,( position.x < (screenResolution.x * 0.15) ).    if (( position.x < 300 && position.y > 380 ) && ( deviceType != PointerDeviceType::Mouse ))    {        if ( !m_moveInUse ) // if no pointer is in this control yet        {            // Process a DPad touch down event.            m_moveFirstDown = position;                 // Save the location of the initial contact.            m_movePointerposition = position;            m_movePointerID = pointerID;                // Store the ID of the pointer using this control.            m_moveInUse = TRUE;        }    }    else // This pointer must be in the look control.    {        if ( !m_lookInUse ) // If no pointer is in this control yet...        {            m_lookLastPoint = position;                         // save the point for later move            m_lookPointerID = args->CurrentPoint->PointerID;  // store the ID of pointer using this control            m_lookLastDelta.x = m_lookLastDelta.y = 0;          // these are for smoothing            m_lookInUse = TRUE;        }    }}

我的问题是:

>对于“On”前缀的使用确实存在这样的模糊性,还是仅仅是我的误解?在提升和处理事件方法时,人们真的使用“On”吗?
>实施方法提升和处理事件的标准方式是什么?什么是流行的风格?你建议的风格是什么?

解决方法 对于引发事件的类:当出现“某些条件”时,在该类中调用OnSomeCondition()方法是有意义的.然后,如果您想通知外部方关于此情况,您将在OnSomeCondition()方法中引发一个事件SomeCondition.

对于处理事件的类:当Visual Studio自动生成处理程序方法时,它会将其称为someClass_SomeCondition(至少在C#中,这是您标记的问题).第二个文档exerpt和示例不是C#,这可以解释差异(我不知道是否存在事件处理程序的“官方”命名约定).

但是,当您从引发事件的类继承并且基类遵循受保护的虚拟建议时,单词“event”变得不明确:您仍然可以处理SomeCondition事件,但您也可以选择覆盖OnSomeCondition()方法.

所以我不会说On前缀“用于引发事件”,而是“处理条件”,你可以选择在OnCondition()方法中引发一个事件 – 对于消费方,你要么处理事件或覆盖OnCondition()行为.这也是第一个文档exerpt所说的:

You provIDe this method to enable derived classes to overrIDe the logic for raising the event.

总结

以上是内存溢出为你收集整理的在C#编码的事件情况下,前缀“On”的实现是什么?全部内容,希望文章能够帮你解决在C#编码的事件情况下,前缀“On”的实现是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1232541.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存