ChtmlTextWriter Construtores

Definição

Inicializa uma nova instância da ChtmlTextWriter classe.

Sobrecargas

Name Description
ChtmlTextWriter(TextWriter)

Inicializa uma nova instância da ChtmlTextWriter classe que usa a DefaultTabString constante para indentar linhas.

ChtmlTextWriter(TextWriter, String)

Inicializa uma nova instância da ChtmlTextWriter classe com a indentação de linha especificada.

ChtmlTextWriter(TextWriter)

Inicializa uma nova instância da ChtmlTextWriter classe que usa a DefaultTabString constante para indentar linhas.

public:
 ChtmlTextWriter(System::IO::TextWriter ^ writer);
public ChtmlTextWriter(System.IO.TextWriter writer);
new System.Web.UI.ChtmlTextWriter : System.IO.TextWriter -> System.Web.UI.ChtmlTextWriter
Public Sub New (writer As TextWriter)

Parâmetros

writer
TextWriter

O TextWriter que gera o conteúdo da marcação.

Exemplos

O exemplo de código seguinte demonstra como criar uma classe nomeada ChtmlCustomPageAdapter e define um método, CreateCustomChtmlTextWriter, que cria e devolve uma instância da CustomChtmlTextWriter classe. Depois CustomChtmlTextWriter , renderiza conteúdo cHTML para páginas em dispositivos com navegadores que usam marcação cHTML.

Este exemplo de código faz parte de um exemplo maior fornecido para a ChtmlTextWriter classe.

// Derive from the WebControlAdapter class,
// provide a CreateCustomChtmlTextWriter
// method to attach to the custom writer.
public class ChtmlCustomPageAdapter : WebControlAdapter
{
    protected internal ChtmlTextWriter CreateCustomChtmlTextWriter(
        TextWriter writer)
    {
        return new CustomChtmlTextWriter(writer);
    }
}
' Derive from the WebControlAdapter class,
' provide a CreateCustomChtmlTextWriter
' method to attach the custom writer.
Public Class ChtmlCustomPageAdapter
    Inherits WebControlAdapter

    Protected Friend Function CreateCustomChtmlTextWriter( _
     ByVal writer As TextWriter) As ChtmlTextWriter

        Return New CustomChtmlTextWriter(writer)

    End Function
End Class

Observações

A ChtmlTextWriter classe tem dois construtores, o que é padrão para todas as classes que derivam direta ou indiretamente da HtmlTextWriter classe.

O ChtmlTextWriter construtor, que toma uma instância da TextWriter classe como parâmetro, chama o segundo construtor e passa-lhe dois valores de parâmetro:

  • O TextWriter.
  • O valor da cadeia especificada no DefaultTabString campo, que define o espaçamento de tabulação utilizado pelo escritor de texto XHTML.

Aplica-se a

ChtmlTextWriter(TextWriter, String)

Inicializa uma nova instância da ChtmlTextWriter classe com a indentação de linha especificada.

public:
 ChtmlTextWriter(System::IO::TextWriter ^ writer, System::String ^ tabString);
public ChtmlTextWriter(System.IO.TextWriter writer, string tabString);
new System.Web.UI.ChtmlTextWriter : System.IO.TextWriter * string -> System.Web.UI.ChtmlTextWriter
Public Sub New (writer As TextWriter, tabString As String)

Parâmetros

writer
TextWriter

O TextWriter que gera o conteúdo da marcação.

tabString
String

O número de espaços definidos no Indent.

Exemplos

O exemplo de código seguinte demonstra como criar uma classe personalizada chamada CustomChtmlTextWriter que deriva da ChtmlTextWriter classe. Cria dois construtores que podes usar para criar uma instância da classe personalizada com o mesmo padrão que todas as classes que derivam, direta ou indiretamente, da HtmlTextWriter classe.

// Create a class that derives from the
// ChtmlTextWriter class.
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls.Adapters;

namespace AspNet.Samples.CS
{
    public class CustomChtmlTextWriter : ChtmlTextWriter
    {
        // Create two constructors for the new
        // text writer.
        public CustomChtmlTextWriter(TextWriter writer) : base(writer, DefaultTabString)
        {
        }

        public CustomChtmlTextWriter(TextWriter writer, String tabString)
            : base(writer, tabString)
        {
        }
        
        // Override the OnAttributeRender method to
        // not render the bgcolor attribute, which is
        // not supported in CHTML.
        protected override bool OnAttributeRender(string name, string value, HtmlTextWriterAttribute key)
        {
            if (String.Equals("bgcolor", name))
            {
                return false;
            }
            
            // Call the ChtmlTextWriter version of the
            // the OnAttributeRender method.
            return base.OnAttributeRender(name, value, key);
        }
    }

    // Derive from the WebControlAdapter class,
    // provide a CreateCustomChtmlTextWriter
    // method to attach to the custom writer.
    public class ChtmlCustomPageAdapter : WebControlAdapter
    {
        protected internal ChtmlTextWriter CreateCustomChtmlTextWriter(
            TextWriter writer)
        {
            return new CustomChtmlTextWriter(writer);
        }
    }
}
' Create a class that derives from the
' ChtmlTextWriter class.
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls.Adapters

Namespace AspNet.Samples.VB

    Public Class CustomChtmlTextWriter
        Inherits ChtmlTextWriter

        ' Create two constructors for the new
        ' text writer.
        Public Sub New(ByVal writer As TextWriter)
            MyClass.New(writer, DefaultTabString)
        End Sub

        Public Sub New(ByVal writer As TextWriter, ByVal tabString As String)
            MyBase.New(writer, tabString)
        End Sub

        ' Override the OnAttributeRender method to
        ' not render the bgcolor attribute, which is 
        ' not supported in CHTML.
        Protected Overrides Function OnAttributeRender(ByVal name As String, ByVal value As String, ByVal key As HtmlTextWriterAttribute) As Boolean
            If (String.Equals("bgcolor", name)) Then
                Return False
            End If

            ' Call the ChtmlTextWriter version of 
            ' the OnAttributeRender method.
            MyBase.OnAttributeRender(name, value, key)

        End Function
    End Class

    ' Derive from the WebControlAdapter class,
    ' provide a CreateCustomChtmlTextWriter
    ' method to attach the custom writer.
    Public Class ChtmlCustomPageAdapter
        Inherits WebControlAdapter

        Protected Friend Function CreateCustomChtmlTextWriter( _
         ByVal writer As TextWriter) As ChtmlTextWriter

            Return New CustomChtmlTextWriter(writer)

        End Function
    End Class
End Namespace

Observações

O ChtmlTextWriter construtor, que toma tanto uma instância da TextWriter classe como uma cadeia como parâmetros, chama o Html32TextWriter construtor que toma os mesmos parâmetros quando cria uma instância da ChtmlTextWriter classe.

Aplica-se a