TextPointer.GetNextContextPosition(LogicalDirection) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengembalikan penunjuk ke simbol berikutnya dalam arah logika yang ditentukan.
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
Parameter
- direction
- LogicalDirection
Salah LogicalDirection satu nilai yang menentukan arah logis untuk mencari simbol berikutnya.
Mengembalikan
Ke TextPointer simbol berikutnya dalam arah yang diminta, atau null jika saat ini TextPointer berbatasan dengan awal atau akhir konten.
Contoh
Contoh berikut menunjukkan penggunaan untuk metode ini. Contoh menggunakan metode bersama GetNextContextPosition dengan GetPointerContext metode untuk melintasi dan mengekstrak simbol dalam .TextElement
Meskipun contoh dapat digunakan untuk mengekstrak struktur XAML untuk konten yang diberikan TextElement, contoh tersebut hanya ditujukan untuk tujuan ilustrasi, dan tidak boleh digunakan dalam kode produksi. System.Xml Lihat namespace layanan untuk sekumpulan jenis kaya yang dirancang untuk bekerja dengan dan memproses 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.
Keterangan
Salah satu hal berikut dianggap sebagai simbol:
Tag pembuka atau penutup untuk TextElement elemen.
UIElement Elemen yang terkandung dalam InlineUIContainer atau BlockUIContainer. Perhatikan bahwa seperti UIElement itu selalu dihitung sebagai tepat satu simbol; konten atau elemen tambahan yang terkandung oleh UIElement tidak dihitung sebagai simbol.
Karakter Unicode 16-bit di dalam elemen teks Run .
Jika simbol berikutnya dikategorikan sebagai EmbeddedElement, , atau ElementEnd (seperti yang diidentifikasi oleh GetPointerContext metode ), maka TextPointer yang dikembalikan oleh metode ini dimajukan dengan tepat satu simbol dari ElementStartposisi saat ini.
Jika simbol berikutnya dikategorikan sebagai Text, maka TextPointer yang dikembalikan oleh metode ini maju melewati teks ke simbol non-teks berikutnya (yaitu, posisi berikutnya di mana TextPointerContext tidak Text). Jumlah simbol yang tepat yang disilangkan dapat dihitung terlebih dahulu dengan memanggil GetTextRunLength metode .