RecognizerContext.Recognition 事件

RecognizerContext 对象通过 BackgroundRecognize 方法生成结果之后发生。

命名空间:  Microsoft.Ink
程序集:  Microsoft.Ink(在 Microsoft.Ink.dll 中)

语法

声明
Public Event Recognition As RecognizerContextRecognitionEventHandler
用法
Dim instance As RecognizerContext
Dim handler As RecognizerContextRecognitionEventHandler

AddHandler instance.Recognition, handler
public event RecognizerContextRecognitionEventHandler Recognition
public:
 event RecognizerContextRecognitionEventHandler^ Recognition {
    void add (RecognizerContextRecognitionEventHandler^ value);
    void remove (RecognizerContextRecognitionEventHandler^ value);
}
/** @event */
public void add_Recognition (RecognizerContextRecognitionEventHandler value)
/** @event */
public void remove_Recognition (RecognizerContextRecognitionEventHandler value)
JScript 不支持事件。

备注

事件处理程序接收 RecognizerContextRecognitionEventArgs 类型的参数,该参数包含有关此事件的数据。

创建 RecognizerContextRecognitionEventHandler 委托时,需要标识将处理该事件的方法。若要将该事件与事件处理程序关联,请将该委托的一个实例添加到事件中。除非移除了该委托,否则每当发生该事件时就会调用此事件处理程序。

如果尝试从识别事件处理程序访问原始 RecognizerContext 对象,则应用程序编程接口 (API) 的行为将不可预知。请不要尝试这么做。如果确实需要,正确的做法是在 Recognition 事件处理程序中创建一个标志并对其进行设置。然后,可以在该事件处理程序外部轮询该标志以确定何时更改 RecognizerContext 属性。

示例

此示例自动识别在 InkOverlay 对象中创建的每个笔画,并显示识别结果。

在应用程序启动期间,将实例化 RecognizerContext 对象并分配事件处理程序。

' create a new RecognizerContext object
' the object's Strokes property is initialized to null
mRecognizerContext = New RecognizerContext()
' assign the Strokes property by creating a fresh Strokes collection 
mRecognizerContext.Strokes = mInkOverlay.Ink.CreateStrokes()
' subscribe to the Strokes event. It is during this event
' that we can add strokes to the RecognizerContext
AddHandler mInkOverlay.Stroke, New InkCollectorStrokeEventHandler(AddressOf mInkOverlay_Stroke2)
' subscribe to the the Recognition event. 
' This event is fired when background recognition is complete, 
' and recognition results (without alternates) are available
AddHandler mRecognizerContext.Recognition, _
        New RecognizerContextRecognitionEventHandler(AddressOf mRecognizerContext_Recognition)
// create a new RecognizerContext object
// the object's Strokes property is initialized to null
mRecognizerContext = new RecognizerContext();
// assign the Strokes property by creating a fresh Strokes collection 
mRecognizerContext.Strokes = mInkOverlay.Ink.CreateStrokes();
// subscribe to the Strokes event. It is during this event
// that we can add strokes to the RecognizerContext
mInkOverlay.Stroke += new InkCollectorStrokeEventHandler(mInkOverlay_Stroke2);
// subscribe to the the Recognition event. 
// This event is fired when background recognition is complete, 
// and recognition results (without alternates) are available
mRecognizerContext.Recognition += 
    new RecognizerContextRecognitionEventHandler(mRecognizerContext_Recognition);

在激发 Stroke 事件(对用户完成笔画做出的响应)时,会将新创建的笔画添加到 RecognizerContext 对象的 Strokes 集合中,并调用 BackgroundRecognize 方法。

Private Sub mInkOverlay_Stroke2(ByVal sender As Object, ByVal e As InkCollectorStrokeEventArgs)
    ' in case background recognition is still occurring, stop it
    mRecognizerContext.StopBackgroundRecognition()
    ' add the stroke, and start recognition
    mRecognizerContext.Strokes.Add(e.Stroke)
    mRecognizerContext.BackgroundRecognize()
End Sub
private void mInkOverlay_Stroke2(object sender, InkCollectorStrokeEventArgs e)
{
    // in case background recognition is still occurring, stop it
    mRecognizerContext.StopBackgroundRecognition();
    // add the stroke, and start recognition
    mRecognizerContext.Strokes.Add(e.Stroke);
    mRecognizerContext.BackgroundRecognize();
}

完成后台识别时将激发 Recognition 事件。在处理此事件的过程中,会将识别结果放入一个列表框。

' event fires when recognition results (without alternates) are ready
Private Sub mRecognizerContext_Recognition(ByVal sender As Object, _
            ByVal e As RecognizerContextRecognitionEventArgs)
    ' when updating a control, must use Invoke() since controls are
    ' not thread safe and recognition occurs on a different thread
    If Me.InvokeRequired Then
        ' recursively call this method via Invoke()
        Me.Invoke( _
            New RecognizerContextRecognitionEventHandler(AddressOf mRecognizerContext_Recognition), _
            New Object() {sender, e} _
        )
        Return
    End If
    If RecognitionStatus.NoError = e.RecognitionStatus Then
        listBoxRecognitionResults.Items.Add(e.Text)
    End If
End Sub
// event fires when recognition results (without alternates) are ready
private void mRecognizerContext_Recognition(object sender, RecognizerContextRecognitionEventArgs e)
{
    // when updating a control, must use Invoke() since controls are
    // not thread safe and recognition occurs on a different thread
    if (this.InvokeRequired)
    {
        // recursively call this method via Invoke()
        this.Invoke(
            new RecognizerContextRecognitionEventHandler(mRecognizerContext_Recognition), 
            new object[] { sender, e }
            );
        return;
    }
    if (RecognitionStatus.NoError == e.RecognitionStatus)
    {
        listBoxRecognitionResults.Items.Add(e.Text);
    }
}

平台

Windows Vista

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

版本信息

.NET Framework

受以下版本支持:3.0

另请参见

参考

RecognizerContext 类

RecognizerContext 成员

Microsoft.Ink 命名空间

RecognitionResult

RecognitionStatus

RecognizerContext.BackgroundRecognize

RecognizerContext.Recognize