다음을 통해 공유


ContextNode 클래스

업데이트: 2007년 11월

잉크 분석 도중 만들어진 개체 트리의 노드를 나타냅니다.

네임스페이스:  System.Windows.Ink
어셈블리:  IAWinFX(IAWinFX.dll)

구문

‘선언
Public Class ContextNode
‘사용 방법
Dim instance As ContextNode
public class ContextNode
public ref class ContextNode
public class ContextNode
public class ContextNode

설명

InkAnalyzer에서는 잉크 분석을 수행한 후 결과를 ContextNode 개체 트리로 나타냅니다. RootNode는 트리의 맨 위에 있습니다. 트리 아래쪽으로 갈수록 점점 더 작은 스트로크 컬렉션이 표시됩니다. 이러한 노드는 단락이나 줄 등의 단어 그룹, 단어나 그리기 등의 분석된 잉크 또는 다양한 기타 형식일 수 있습니다. 전체 목록을 보려면 ContextNodeType 클래스를 참조하십시오.

AnalysisHintNode, ImageNodeTextWordNode 개체와 같은 노드를 InkAnalyzer에 수동으로 추가할 수 있습니다.

입력이 손상된 경우 이 메서드는 ArgumentException 예외를 throw합니다.

예제

이 예제에서는 이름이 theInkAnalyzer인 InkAnalyzer의 ContextNode 트리를 사용하여 이름이 theTreeView인 [System.Windows.Controls.TreeView]를 채웁니다. 트리 뷰에 속한 노드를 선택하면 연관된 스트로크가 빨간색으로 표시되도록 설정됩니다. [System.Windows.FrameworkElement.Tag] 속성은 트리 노드와 해당 노드가 나타내는 컨텍스트 노드를 서로 매핑하는 데 사용됩니다.

Private Sub BuildTree() 
    ' Remove the old nodes from the TreeView and add the current results.
    Me.theResultsTreeView.Items.Clear()

    Dim rootNode As New TreeViewItem()
    rootNode.Tag = Me.theInkAnalyzer.RootNode
    rootNode.Header = theInkAnalyzer.RootNode.ToString()

    Me.theResultsTreeView.Items.Add(rootNode)

    WalkTree(Me.theInkAnalyzer.RootNode, rootNode)

End Sub 'BuildTree


Private Shared Sub WalkTree(ByVal parentContextNode As ContextNode, ByVal parentTreeNode As TreeViewItem) 

    parentTreeNode.IsExpanded = True

    For Each theContextSubnode As ContextNode In parentContextNode.SubNodes
        Dim newTreeNode As New TreeViewItem()
        newTreeNode.Header = theContextSubnode.ToString()

        If TypeOf theContextSubnode Is InkWordNode Then
            newTreeNode.Header += ": " + CType(theContextSubnode, InkWordNode).GetRecognizedString()
        ElseIf TypeOf theContextSubnode Is InkDrawingNode Then
            newTreeNode.Header += ": " + CType(theContextSubnode, InkDrawingNode).GetShapeName()
        End If

        ' If the context node is confirmed, add a note to the
        ' tree view item.
        If (theContextSubnode.IsConfirmed(ConfirmationType.NodeTypeAndProperties)) Then

            newTreeNode.Header += " Confirmed."
        End If


        ' Add the context node as a tag of the tree view item and
        ' add the new tree view item to the parent node.
        newTreeNode.Tag = theContextSubnode
        parentTreeNode.Items.Add(newTreeNode)

        WalkTree(theContextSubnode, newTreeNode)
    Next theContextSubnode

End Sub 'WalkTree

Sub theResultsTreeView_SelectedItemChanged(ByVal sender As Object, _
                                           ByVal e As RoutedPropertyChangedEventArgs(Of Object))
    Dim selectedTreeViewItem As TreeViewItem = e.NewValue '

    If selectedTreeViewItem Is Nothing Then
        Return
    End If

    ' Get the context node
    Dim selectedNode As ContextNode = CType(selectedTreeViewItem.Tag, ContextNode)

    MarkNodeAsRed(selectedNode)

    timeStampLabel.Content = ""

    ' Show selected results
    If Not (selectedNode Is Nothing) Then
        If selectedNode.Type = ContextNodeType.WritingRegion Then
            Dim writingRegion As WritingRegionNode = CType(selectedNode, WritingRegionNode)
            selectedResultsTextBox.Text = writingRegion.GetRecognizedString()
        ElseIf selectedNode.Type = ContextNodeType.Paragraph Then
            Dim paragraph As ParagraphNode = CType(selectedNode, ParagraphNode)
            selectedResultsTextBox.Text = paragraph.GetRecognizedString()
        ElseIf selectedNode.Type = ContextNodeType.Line Then
            Dim line As LineNode = CType(selectedNode, LineNode)
            selectedResultsTextBox.Text = line.GetRecognizedString()
        ElseIf selectedNode.Type = ContextNodeType.InkWord Then
            Dim inkWord As InkWordNode = CType(selectedNode, InkWordNode)
            Dim parentNode As ContextNode = inkWord.ParentNode
            If TypeOf parentNode Is LineNode Then
                Dim parentLine As LineNode = CType(parentNode, LineNode)
                ' Put parent line's recognized string into the text box
                selectedResultsTextBox.Text = parentLine.GetRecognizedString()

                ' Select the text that corresponds to the ink word
                Dim subNodes As New ContextNodeCollection(theInkAnalyzer)
                subNodes.Add(inkWord)
                Dim start As Integer
                Dim length As Integer
                parentLine.GetTextRangeFromNodes(subNodes, start, length)
                If start >= 0 AndAlso length > 0 Then
                    selectedResultsTextBox.Select(start, length)
                End If
            End If
        ElseIf selectedNode.Type = ContextNodeType.InkDrawing Then
            Dim drawingNode As InkDrawingNode = CType(selectedNode, InkDrawingNode)
            selectedResultsTextBox.Text = drawingNode.GetShapeName()
        ElseIf selectedNode.Type = ContextNodeType.InkBullet Then
            Dim bulletNode As InkBulletNode = CType(selectedNode, InkBulletNode)
            selectedResultsTextBox.Text = bulletNode.GetRecognizedString()
        ElseIf selectedNode.Type = ContextNodeType.CustomRecognizer Then
            Dim customRecognizer As CustomRecognizerNode = CType(selectedNode, CustomRecognizerNode)
            selectedResultsTextBox.Text = customRecognizer.GetRecognizedString()
        ElseIf selectedNode.Type = ContextNodeType.Object Then
            Dim selectedObject As ObjectNode = CType(selectedNode, ObjectNode)
            selectedResultsTextBox.Text = selectedObject.GetRecognizedString()
        Else
            selectedResultsTextBox.Text = ""
        End If
        If TypeOf selectedNode Is InkWordNode Then
            Dim inkWord As InkWordNode = CType(selectedNode, InkWordNode)

            ' Show the time stamp
            If inkWord.ContainsPropertyData(Me.timeStampGuid) Then
                Dim timeStamp As DateTime = CType(inkWord.GetPropertyData(Me.timeStampGuid), DateTime)
                timeStampLabel.Content = timeStamp.ToShortTimeString()
            End If
            ' Snippet to demonstrate GetPropertyDataIds
            Dim propertyDataIds As Guid() = inkWord.GetPropertyDataIds()
            ' Snippets to demonstrate loading and saving
            Dim data As Byte() = inkWord.SavePropertiesData()
            If Not inkWord.LoadPropertiesData(data) Then
                MessageBox.Show("Cannot load property data")
            End If
        End If
    End If
    PopulateLinksList(selectedNode)
    Me.currentNode = selectedNode
End Sub

Private Sub PopulateLinksList(ByVal selectedNode As ContextNode)

    linksListBox.Items.Clear()

    If selectedNode Is Nothing Then
        Return
    End If

    Dim link As ContextLink
    For Each link In selectedNode.Links
        linksListBox.Items.Add(link.SourceNode.ToString() + ", " & _
                                    link.DestinationNode.ToString() & ": " & _
                                    link.LinkDirection.ToString())
    Next
End Sub

Sub MarkNodeAsRed(ByVal selectedNode As ContextNode)
    ' Set all node strokes to black, but this one to red
    Dim stroke As Stroke
    For Each stroke In Me.theInkCanvas.Strokes
        If Not (selectedNode Is Nothing) AndAlso selectedNode.Strokes.Contains(stroke) Then
            stroke.DrawingAttributes.Color = Colors.Red
        Else
            stroke.DrawingAttributes.Color = Me.theInkCanvas.DefaultDrawingAttributes.Color
        End If
    Next stroke

End Sub 'theResultsTreeView_SelectedItemChanged 
private void BuildTree()
{
    // Remove the old nodes from the TreeView and add the current results.
    this.theResultsTreeView.Items.Clear();

    TreeViewItem rootNode = new TreeViewItem();
    rootNode.Tag = this.theInkAnalyzer.RootNode;
    rootNode.Header = theInkAnalyzer.RootNode.ToString();

    this.theResultsTreeView.Items.Add(rootNode);

    WalkTree(this.theInkAnalyzer.RootNode, rootNode);

}

private static void WalkTree(ContextNode parentContextNode, TreeViewItem parentTreeNode)
{
    // Expand the current TreeViewItem.
    parentTreeNode.IsExpanded = true;

    foreach (ContextNode theContextSubnode
        in parentContextNode.SubNodes)
    {
        TreeViewItem newTreeNode = new TreeViewItem();
        newTreeNode.Header = theContextSubnode.ToString();

        if (theContextSubnode is InkWordNode)
        {
            newTreeNode.Header += ": " +
                ((InkWordNode)theContextSubnode).GetRecognizedString();
        }
        else if (theContextSubnode is InkDrawingNode)
        {
            newTreeNode.Header += ": " +
                ((InkDrawingNode)theContextSubnode).GetShapeName();
        }

        // If the context node is confirmed, add a note to the
        // tree view item.
        if (theContextSubnode.IsConfirmed(ConfirmationType.NodeTypeAndProperties))
        {
            newTreeNode.Header += " Confirmed.";
        }

        // Add the context node as a tag of the tree view item and
        // add the new tree view item to the parent node.
        newTreeNode.Tag = theContextSubnode;
        parentTreeNode.Items.Add(newTreeNode);

        WalkTree(theContextSubnode, newTreeNode);
    }
}


void theResultsTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    TreeViewItem selectedTreeViewItem = e.NewValue as TreeViewItem;

    if (selectedTreeViewItem == null)
    {
        return;
    }

    // Get the context node
    ContextNode selectedNode = (ContextNode)selectedTreeViewItem.Tag;

    MarkNodeAsRed(selectedNode);

    timeStampLabel.Content = "";

    // Show selected results
    if (selectedNode != null)
    {
        if (selectedNode.Type == ContextNodeType.WritingRegion)
        {
            WritingRegionNode writingRegion = (WritingRegionNode)selectedNode;
            selectedResultsTextBox.Text = writingRegion.GetRecognizedString();
        }
        else if (selectedNode.Type == ContextNodeType.Paragraph)
        {
            ParagraphNode paragraph = (ParagraphNode)selectedNode;
            selectedResultsTextBox.Text = paragraph.GetRecognizedString();
        }
        else if (selectedNode.Type == ContextNodeType.Line)
        {
            LineNode line = (LineNode)selectedNode;
            selectedResultsTextBox.Text = line.GetRecognizedString();
        }
        else if (selectedNode.Type == ContextNodeType.InkWord)
        {
            InkWordNode inkWord = (InkWordNode)selectedNode;
            ContextNode parentNode = inkWord.ParentNode;
            if (parentNode is LineNode)
            {
                LineNode parentLine = (LineNode)parentNode;
                // Put parent line's recognized string into the text box
                selectedResultsTextBox.Text = parentLine.GetRecognizedString();

                // Select the text that corresponds to the ink word
                ContextNodeCollection subNodes = new ContextNodeCollection(theInkAnalyzer);
                subNodes.Add(inkWord);
                int start;
                int length;
                parentLine.GetTextRangeFromNodes(subNodes, out start, out length);
                if (start >= 0 && length > 0)
                {
                    selectedResultsTextBox.Select(start, length);
                }
            }
        }
        else if (selectedNode.Type == ContextNodeType.InkDrawing)
        {
            InkDrawingNode drawingNode = (InkDrawingNode)selectedNode;
            selectedResultsTextBox.Text = drawingNode.GetShapeName();
        }
        else if (selectedNode.Type == ContextNodeType.InkBullet)
        {
            InkBulletNode bulletNode = (InkBulletNode)selectedNode;
            selectedResultsTextBox.Text = bulletNode.GetRecognizedString();
        }
        else if (selectedNode.Type == ContextNodeType.CustomRecognizer)
        {
            CustomRecognizerNode customRecognizer = (CustomRecognizerNode)selectedNode;
            selectedResultsTextBox.Text = customRecognizer.GetRecognizedString();
        }
        else if (selectedNode.Type == ContextNodeType.Object)
        {
            ObjectNode selectedObject = (ObjectNode)selectedNode;
            selectedResultsTextBox.Text = selectedObject.GetRecognizedString();
        }
        else
        {
            selectedResultsTextBox.Text = "";
        }

        if (selectedNode is InkWordNode)
        {
            InkWordNode inkWord = (InkWordNode)selectedNode;

            // Show the time stamp
            if (inkWord.ContainsPropertyData(this.timeStampGuid))
            {
                DateTime timeStamp =
                    (DateTime)inkWord.GetPropertyData(this.timeStampGuid);
                timeStampLabel.Content = timeStamp.ToShortTimeString();
            }

            // Snippet to demonstrate GetPropertyDataIds
            Guid[] propertyDataIds = inkWord.GetPropertyDataIds();
            // Snippets to demonstrate loading and saving
            byte[] data = inkWord.SavePropertiesData();
            if (!inkWord.LoadPropertiesData(data))
                MessageBox.Show("Cannot load property data");
        }

    }
    PopulateLinksList(selectedNode);
    this.currentNode = selectedNode;
}

private void PopulateLinksList(ContextNode selectedNode)
{
    linksListBox.Items.Clear();
    if (selectedNode == null)
    {
        return;
    }

    foreach (ContextLink link in selectedNode.Links)
    {
        linksListBox.Items.Add(link.SourceNode.ToString() + ", " +
                                    link.DestinationNode.ToString() + ": " +
                                    link.LinkDirection.ToString());
    }
}

private void MarkNodeAsRed(ContextNode selectedNode)
{
    // Set all node strokes to black, but this one to red
    foreach (Stroke stroke in this.theInkCanvas.Strokes)
    {
        if (selectedNode != null &&
            selectedNode.Strokes.Contains(stroke))
            stroke.DrawingAttributes.Color = Colors.Red;
        else
            stroke.DrawingAttributes.Color = 
                this.theInkCanvas.DefaultDrawingAttributes.Color;
    }
}

상속 계층 구조

System.Object
  System.Windows.Ink.ContextNode
    System.Windows.Ink.AnalysisHintNode
    System.Windows.Ink.CustomRecognizerNode
    System.Windows.Ink.ImageNode
    System.Windows.Ink.InkBulletNode
    System.Windows.Ink.InkDrawingNode
    System.Windows.Ink.InkWordNode
    System.Windows.Ink.LineNode
    System.Windows.Ink.ObjectNode
    System.Windows.Ink.ParagraphNode
    System.Windows.Ink.RootNode
    System.Windows.Ink.TextWordNode
    System.Windows.Ink.UnclassifiedInkNode
    System.Windows.Ink.WritingRegionNode

스레드로부터의 안전성

이 형식의 모든 공용 static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows Vista

.NET Framework 및 .NET Compact Framework에서 모든 플랫폼의 전체 버전을 지원하지는 않습니다. 지원되는 버전의 목록을 보려면 .NET Framework 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

3.0에서 지원

참고 항목

참조

ContextNode 멤버

System.Windows.Ink 네임스페이스

InkAnalyzer.Analyze

InkAnalyzer.RootNode

System.Windows.Ink.ContextNodeType