TextPointer.GetNextContextPosition(LogicalDirection) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Returnerar en pekare till nästa symbol i den angivna logiska riktningen.
public:
System::Windows::Documents::TextPointer ^ GetNextContextPosition(System::Windows::Documents::LogicalDirection direction);
public System.Windows.Documents.TextPointer GetNextContextPosition(System.Windows.Documents.LogicalDirection direction);
member this.GetNextContextPosition : System.Windows.Documents.LogicalDirection -> System.Windows.Documents.TextPointer
Public Function GetNextContextPosition (direction As LogicalDirection) As TextPointer
Parametrar
- direction
- LogicalDirection
Ett av de LogicalDirection värden som anger den logiska riktning som du vill söka efter nästa symbol i.
Returer
A TextPointer till nästa symbol i den begärda riktningen, eller null om den aktuella TextPointer kantlinjen är början eller slutet av innehållet.
Exempel
I följande exempel visas en användning för den här metoden. I exemplet används GetNextContextPosition metoden tillsammans med GetPointerContext metoden för att bläddra igenom och extrahera symbolerna i en angiven TextElement.
Även om exemplet kan användas för att extrahera en XAML-struktur för innehållet i en viss TextElement, är den endast avsedd för illustrativa ändamål och bör inte användas i produktionskod. System.Xml Se namnområdet för en omfattande uppsättning typer som är utformade för att arbeta med och bearbeta XML.
// This method will extract and return a string that contains a representation of
// the XAML structure of content elements in a given TextElement.
string GetXaml(TextElement element)
{
StringBuilder buffer = new StringBuilder();
// Position a "navigator" pointer before the opening tag of the element.
TextPointer navigator = element.ElementStart;
while (navigator.CompareTo(element.ElementEnd) < 0)
{
switch (navigator.GetPointerContext(LogicalDirection.Forward))
{
case TextPointerContext.ElementStart :
// Output opening tag of a TextElement
buffer.AppendFormat("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
break;
case TextPointerContext.ElementEnd :
// Output closing tag of a TextElement
buffer.AppendFormat("</{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
break;
case TextPointerContext.EmbeddedElement :
// Output simple tag for embedded element
buffer.AppendFormat("<{0}/>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
break;
case TextPointerContext.Text :
// Output the text content of this text run
buffer.Append(navigator.GetTextInRun(LogicalDirection.Forward));
break;
}
// Advance the naviagtor to the next context position.
navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
} // End while.
return buffer.ToString();
} // End GetXaml method.
' This method will extract and return a string that contains a representation of
' the XAML structure of content elements in a given TextElement.
Private Function GetXaml_Renamed(ByVal element As TextElement) As String
Dim buffer As New StringBuilder()
' Position a "navigator" pointer before the opening tag of the element.
Dim navigator As TextPointer = element.ElementStart
Do While navigator.CompareTo(element.ElementEnd) < 0
Select Case navigator.GetPointerContext(LogicalDirection.Forward)
Case TextPointerContext.ElementStart
' Output opening tag of a TextElement
buffer.AppendFormat("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name)
Case TextPointerContext.ElementEnd
' Output closing tag of a TextElement
buffer.AppendFormat("</{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name)
Case TextPointerContext.EmbeddedElement
' Output simple tag for embedded element
buffer.AppendFormat("<{0}/>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name)
Case TextPointerContext.Text
' Output the text content of this text run
buffer.Append(navigator.GetTextInRun(LogicalDirection.Forward))
End Select
' Advance the naviagtor to the next context position.
navigator = navigator.GetNextContextPosition(LogicalDirection.Forward)
Loop ' End while.
Return buffer.ToString()
End Function ' End GetXaml method.
Kommentarer
Något av följande anses vara en symbol:
En inledande eller avslutande tagg för ett TextElement element.
Ett UIElement element som finns i en InlineUIContainer eller BlockUIContainer. Observera att en UIElement sådan alltid räknas som exakt en symbol. Eventuella ytterligare innehåll eller element som ingår i UIElement räknas inte som symboler.
Ett 16-bitars Unicode-tecken inuti ett textelement Run .
Om nästa symbol kategoriseras som EmbeddedElement, ElementStarteller ElementEnd (som identifieras av GetPointerContext metoden), avanceras den TextPointer som returneras av den här metoden med exakt en symbol från den aktuella positionen.
Om nästa symbol kategoriseras som Text, avanceras den TextPointer som returneras av den här metoden förbi texten till nästa symbol som inte är text (det vill: nästa position där TextPointerContext inte Textär ). Det exakta antalet symboler som korsas kan beräknas i förväg genom att anropa GetTextRunLength metoden.