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

我认为使用“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.

点赞