RecognizerContext.RecognitionWithAlternates 事件

RecognizerContext 对象通过调用 BackgroundRecognizeWithAlternates 方法生成结果之后发生。

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

语法

声明
Public Event RecognitionWithAlternates As RecognizerContextRecognitionWithAlternatesEventHandler
用法
Dim instance As RecognizerContext
Dim handler As RecognizerContextRecognitionWithAlternatesEventHandler

AddHandler instance.RecognitionWithAlternates, handler
public event RecognizerContextRecognitionWithAlternatesEventHandler RecognitionWithAlternates
public:
 event RecognizerContextRecognitionWithAlternatesEventHandler^ RecognitionWithAlternates {
    void add (RecognizerContextRecognitionWithAlternatesEventHandler^ value);
    void remove (RecognizerContextRecognitionWithAlternatesEventHandler^ value);
}
/** @event */
public void add_RecognitionWithAlternates (RecognizerContextRecognitionWithAlternatesEventHandler value)
/** @event */
public void remove_RecognitionWithAlternates (RecognizerContextRecognitionWithAlternatesEventHandler value)
JScript 不支持事件。

备注

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

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

如果尝试通过 RecognitionWithAlternates 事件处理程序访问原始 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_Stroke1)
' subscribe to the the RecognitionWithAlternates event. 
' This event is fired when background recognition is complete, 
' and recognition results (with alternates) are available
AddHandler mRecognizerContext.RecognitionWithAlternates, _
     New RecognizerContextRecognitionWithAlternatesEventHandler(AddressOf mRecognizerContext_RecognitionWithAlternates)
// 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_Stroke1);
// subscribe to the the RecognitionWithAlternates event. 
// This event is fired when background recognition is complete, 
// and recognition results (with alternates) are available
mRecognizerContext.RecognitionWithAlternates += 
    new RecognizerContextRecognitionWithAlternatesEventHandler(mRecognizerContext_RecognitionWithAlternates);

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

Private Sub mInkOverlay_Stroke1(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.BackgroundRecognizeWithAlternates()
End Sub
private void mInkOverlay_Stroke1(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.BackgroundRecognizeWithAlternates();
}

完成后台识别时将激发 RecognitionWithAlternates 事件。在处理此事件的过程中,会将识别结果(包括备选项)放入一个列表框。

' event fires when recognition results (with alternates) are ready
Private Sub mRecognizerContext_RecognitionWithAlternates(ByVal sender As Object, _
            ByVal e As RecognizerContextRecognitionWithAlternatesEventArgs)
    ' 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 RecognizerContextRecognitionWithAlternatesEventHandler(AddressOf mRecognizerContext_RecognitionWithAlternates), _
            New Object() {sender, e} _
        )
        Return
    End If
    If RecognitionStatus.NoError = e.RecognitionStatus Then
        ' show the top alternate
        listBoxRecognitionResults.Items.Add("TOP:" + e.Result.TopAlternate.ToString())
        ' get the rest of the alternates
        Dim allAlternates As RecognitionAlternates = e.Result.GetAlternatesFromSelection()
        ' show each of the other alternates
        For Each oneAlternate As RecognitionAlternate In allAlternates
            listBoxRecognitionResults.Items.Add("ALT:" + oneAlternate.ToString())
        Next
    End If
End Sub
// event fires when recognition results (with alternates) are ready
private void mRecognizerContext_RecognitionWithAlternates(object sender, RecognizerContextRecognitionWithAlternatesEventArgs 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 RecognizerContextRecognitionWithAlternatesEventHandler(mRecognizerContext_RecognitionWithAlternates),
            new object[] { sender, e }
            );
        return;
    }
    if (RecognitionStatus.NoError == e.RecognitionStatus)
    {
        // show the top alternate
        listBoxRecognitionResults.Items.Add("TOP:" + e.Result.TopAlternate.ToString());
        // get the rest of the alternates
        RecognitionAlternates allAlternates = e.Result.GetAlternatesFromSelection();
        // show each of the other alternates
        foreach (RecognitionAlternate oneAlternate in allAlternates)
        {
            listBoxRecognitionResults.Items.Add("ALT:" + oneAlternate.ToString());
        }
    }
}

平台

Windows Vista

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

版本信息

.NET Framework

受以下版本支持:3.0

另请参见

参考

RecognizerContext 类

RecognizerContext 成员

Microsoft.Ink 命名空间

RecognitionResult

RecognizerContext.BackgroundRecognizeWithAlternates

RecognizerContext.Recognize