SupportsPreviewControlAttribute 類別

定義

指出控制項設計工具在設計階段是否需要控制項的預覽執行個體。 此類別無法獲得繼承。

public ref class SupportsPreviewControlAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class SupportsPreviewControlAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type SupportsPreviewControlAttribute = class
    inherit Attribute
Public NotInheritable Class SupportsPreviewControlAttribute
Inherits Attribute
繼承
SupportsPreviewControlAttribute
屬性

範例

下列程式碼範例示範如何使用 屬性標記控制項設計工具 SupportsPreviewControlAttribute 。 程式碼範例會從 Label 類別衍生 ASP.NET 伺服器控制項,並將 ASP.NET 伺服器控制項與自訂控制項設計工具實作產生關聯。 控制項設計工具類別宣告會標示為 SupportsPreviewControl 屬性 true 。 控制項設計工具會覆寫 方法, GetDesignTimeHtml 並以斜體標記括住控制項的設計階段 HTML。

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.Web.UI.WebControls;
using System.Reflection;

namespace ControlDesignerSamples.CS
{
    // Define a simple designer associated with a 
    // simple text web control.
    
    // Mark the designer with the SupportsPreviewControlAttribute set
    // to true.  This means the base.UsePreviewControl returns true,
    // and base.ViewControl returns a temporary preview copy of the control.
    [SupportsPreviewControl(true)]
    public class SimpleTextControlDesigner : TextControlDesigner
    {		
        // Override the base GetDesignTimeHtml method to display 
        // the design time text in italics.
        public override string GetDesignTimeHtml()
        {
            string html = String.Empty;
 
            try
            {
                // Initialize the return string to the default
                // design time html of the base TextControlDesigner.
                html = base.GetDesignTimeHtml();

                // Get the ViewControl for the associated control.
                Label ctrl = (Label)ViewControl;

                ctrl.Style.Add(HtmlTextWriterStyle.FontStyle, "Italic");
                html = base.GetDesignTimeHtml();
            }
            catch (System.Exception e)
            {
               if (String.IsNullOrEmpty(html))
               {
                   html = GetErrorDesignTimeHtml(e);
               }
            }
            
            return html;
        }
    }

    // Derive a simple Web control from Label to render a text string.
    // Associate this control with the SimpleTextControlDesigner.
    [DesignerAttribute("ControlDesignerSamples.CS.SimpleTextControlDesigner"),
    ToolboxData("<{0}:MyLabelControl Runat=\"Server\"><{0}:MyLabelControl>")]
    public class MyLabelControl : Label
    {
        // Use the Label control implementation, but associate
        // the derived class with the custom control designer.
    }
}
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.Design.WebControls
Imports System.Web.UI.WebControls
Imports System.Reflection

Namespace ControlDesignerSamples.VB

    ' Derive a simple Web control from Label to render a text string.
    ' Associate this control with the SimpleTextControlDesigner.
    <DesignerAttribute("ControlDesignerSamples.CS.SimpleTextControlDesigner"), _
    ToolboxData("<{0}:MyLabelControl Runat=""Server""><{0}:MyLabelControl>")> _
    Public Class MyLabelControl
        Inherits Label

        ' Use the Label control implementation, but associate
        ' the derived class with the custom control designer.
    End Class


    ' Mark the designer with the SupportsPreviewControlAttribute set
    ' to true.  This means the base.UsePreviewControl returns true,
    ' and base.ViewControl returns a temporary preview copy of the control.
    <SupportsPreviewControl(True)> _
    Public Class SimpleTextControlDesigner
        Inherits TextControlDesigner

        ' Override the base GetDesignTimeHtml method to display 
        ' the design time text in italics.
        Public Overrides Function GetDesignTimeHtml() As String
            Dim html As String = String.Empty

            Try
                ' Get the ViewControl for the associated control.
                Dim ctrl As Label = CType(ViewControl, Label)

                ' Set the default text, if necessary
                If ctrl.Text.Length = 0 Then
                    ctrl.Text = "Sample Text"
                End If

                ' Set the style to italic
                ctrl.Style.Add(HtmlTextWriterStyle.FontStyle, "italic")

                ' Let the base class create the HTML markup
                html = MyBase.GetDesignTimeHtml()
            Catch ex As Exception
                If String.IsNullOrEmpty(html) Then
                    ' Display the exception message
                    html = GetErrorDesignTimeHtml(ex)
                End If
            End Try

            Return html
        End Function

    End Class
End Namespace

備註

SupportsPreviewControlAttribute 屬性套用至控制項設計工具類別,以指出控制項設計工具所支援的預覽控制項類型。 使用這個屬性來變更設計階段轉譯的預覽控制項,而不會影響相關聯控制項的實際保存實例。

一般而言,您會在宣告衍生自 類別的 ControlDesigner 自訂設計工具類別時指定 SupportsPreviewControlAttribute 。 屬性的 SupportsPreviewControl 屬性值會決定基 ControlDesigner 類中 和 ViewControl 成員的行為 UsePreviewControl SupportsPreviewControlAttribute

SupportsPreviewControl 屬性設定為 true ,表示設計工具使用關聯控制項的暫存複本來產生設計階段 HTML。 暫存控制項的變更不會保存。

SupportsPreviewControl 屬性設定為 false ,表示設計工具會從 ViewControl 方法傳回控制項實例,特別是 Component 屬性。 控制項物件的變更會保存。

例如,類別 CalendarDesigner 會標示為 SupportsPreviewControlAttribute true 。 設計工具會使用預覽控制項搭配自動樣式格式設定工作,讓使用者預覽可以套用至行事曆的各種自動格式設定樣式。 當使用者在使用者介面中選取不同的自動格式樣式時,選取的樣式配置會套用至預覽控制項。 將新樣式套用至預覽控制項並不會變更套用至設計工具中控制項實例的配置 Calendar

SupportsPreviewControlAttribute如果未在控制項設計工具宣告中指定 ,則 ControlDesigner 行為相當於將 SupportsPreviewControl 指定為 false

注意

衍生自 類別的設計 ControlDesigner 工具類別可以覆寫 UsePreviewControlViewControl 成員,並忽略 SupportsPreviewControlAttribute 屬性。 若要判斷 和 UsePreviewControl 的預期行為 ViewControl ,請參閱衍生控制項設計工具類別的參考檔。

如需使用屬性的一般資訊,請參閱 屬性概觀屬性。 如需設計階段屬性的詳細資訊,請參閱 屬性和Design-Time支援

建構函式

SupportsPreviewControlAttribute(Boolean)

初始化 SupportsPreviewControlAttribute 類別的新執行個體,並設定 SupportsPreviewControl 屬性的初始值。

欄位

Default

取得 SupportsPreviewControlAttribute 類別的執行個體,這個執行個體已設定為預設的預覽值。 此欄位為唯讀。

屬性

SupportsPreviewControl

取得值,指出控制項設計工具在設計階段是否需要有暫存預覽控制項。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

決定指定的物件是否將相同的預覽屬性設定表示為 SupportsPreviewControlAttribute 類別目前的執行個體

GetHashCode()

傳回 SupportsPreviewControlAttribute 類別這個執行個體的雜湊程式碼。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

指出 SupportsPreviewControlAttribute 類別目前的執行個體是否設定為預設的預覽屬性值。

Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱