Поделиться через


RecognizerContext.RecognitionWithAlternates - событие

Обновлен: Ноябрь 2007

Occurs when the RecognizerContext object has generated results after calling the BackgroundRecognizeWithAlternates method.

Пространство имен:  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 не поддерживает события.

Заметки

The event handler receives an argument of type RecognizerContextRecognitionWithAlternatesEventArgs containing data about this event.

When you create a RecognizerContextRecognitionWithAlternatesEventHandler delegate, you identify the method that handles the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.

The behavior of the application programming interface (API) is unpredictable if you try to gain access to the original RecognizerContext object from the RecognitionWithAlternates event handler. Do not attempt to do this. Instead, if you need to do this, create a flag and set it in the Recognition event handler. Then you can poll that flag to determine when to change the RecognizerContext properties outside of the event handler.

Примеры

In this example, each stroke made in an InkOverlay object is automatically recognized and the recognition results (including alternates) displayed.

During application startup, the RecognizerContext object is instantiated, and event handlers are assigned.

' 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);

When the Stroke event fires (in response to the user completing a stroke), the newly created stroke is added to the Strokes collection of the RecognizerContext object, and the BackgroundRecognizeWithAlternates method is called.

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();
}

When background recognition is complete, the RecognitionWithAlternates event fires. During handling of this event, the results of the recognition (including alternates) are placed in a list box.

' 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