ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-06 03:07:45  阅读:377  来源: 互联网

标签:c event-handling events coding-style msdn


我认为使用“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,event-handling,events,coding-style,msdn
来源: https://codeday.me/bug/20190706/1393136.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有