逐步解說:使用 DesignerSerializationVisibilityAttribute 序列化標準型別的集合

您的自訂控制項有時候會將集合公開 (Expose) 為屬性。 這個逐步解說示範如何使用 DesignerSerializationVisibilityAttribute 類別控制集合在設計階段序列化的方式。 套用 Content 值至您的集合屬性,能確保序列化該屬性。

若要將此主題中的程式碼複製為一份清單,請參閱 HOW TO:使用 DesignerSerializationVisibilityAttribute 序列化標準型別的集合

注意事項注意事項

根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。 若要變更設定,請從 [工具] 功能表中選取 [匯入和匯出設定]。 如需詳細資訊,請參閱 使用設定

必要條件

若要完成這個逐步解說,您必須要有:

  • 擁有足夠的使用權限,可以在安裝 Visual Studio 的電腦上建立並執行 Windows Form 應用程式專案。

建立具有可序列化集合的控制項

第一步是建立具有可序列化集合做為屬性的控制項。 您可以使用 [集合編輯器] 編輯此集合的內容 (可以從 [屬性] 視窗存取集合編輯器)。

若要建立有序列化集合的控制項

  1. 建立名為 SerializationDemoControlLib 的 Windows 控制項程式庫專案。 如需詳細資訊,請參閱 Windows Control Library Template

  2. 將 UserControl1 重新命名為 SerializationDemoControl。 如需詳細資訊,請參閱 How to: Rename Identifiers

  3. 在 [屬性] 視窗中,將 Padding.All 屬性值設定為 10。

  4. 在 SerializationDemoControl 中放置 TextBox 控制項。

  5. 選取 TextBox 控制項。 在 [屬性] 視窗中,檢視下列屬性。

    屬性

    變更為

    Multiline

    true

    Dock

    Fill

    ScrollBars

    Vertical

    ReadOnly

    true

  6. 在 [程式碼編輯器] 中,在 SerializationDemoControl 中宣告名為 stringsValue 的字串陣列欄位。

    ' This field backs the Strings property.
     Private stringsValue(1) As String
    
    // This field backs the Strings property.
    private String[] stringsValue = new String[1];
    
            // This field backs the Strings property.
        private:
            array<String^>^ stringsValue;
    
    
    
  7. 定義 SerializationDemoControl 上的 Strings 屬性。

注意事項注意事項

Content 值是用來啟用集合的序列化。

' When the DesignerSerializationVisibility attribute has
' a value of "Content" or "Visible" the designer will 
' serialize the property. This property can also be edited 
' at design time with a CollectionEditor.
 <DesignerSerializationVisibility( _
     DesignerSerializationVisibility.Content)> _
 Public Property Strings() As String()
     Get
         Return Me.stringsValue
     End Get
     Set(ByVal value As String())
         Me.stringsValue = Value

         ' Populate the contained TextBox with the values
         ' in the stringsValue array.
         Dim sb As New StringBuilder(Me.stringsValue.Length)

         Dim i As Integer
         For i = 0 To (Me.stringsValue.Length) - 1
             sb.Append(Me.stringsValue(i))
             sb.Append(ControlChars.Cr + ControlChars.Lf)
         Next i

         Me.textBox1.Text = sb.ToString()
     End Set
 End Property
// When the DesignerSerializationVisibility attribute has
// a value of "Content" or "Visible" the designer will 
// serialize the property. This property can also be edited 
// at design time with a CollectionEditor.
[DesignerSerializationVisibility( 
    DesignerSerializationVisibility.Content )]
public String[] Strings
{
    get
    {
        return this.stringsValue;
    }
    set
    {
        this.stringsValue = value;

        // Populate the contained TextBox with the values
        // in the stringsValue array.
        StringBuilder sb = 
            new StringBuilder(this.stringsValue.Length);

        for (int i = 0; i < this.stringsValue.Length; i++)
        {
            sb.Append(this.stringsValue[i]);
            sb.Append("\r\n");
        }

        this.textBox1.Text = sb.ToString();
    }
}
    // When the DesignerSerializationVisibility attribute has
    // a value of "Content" or "Visible" the designer will 
    // serialize the property. This property can also be edited 
    // at design time with a CollectionEditor.
public:
    [DesignerSerializationVisibility(
        DesignerSerializationVisibility::Content)]
    property array<String^>^ Strings
    {
        array<String^>^ get()
        {
            return this->stringsValue;
        }
        void set(array<String^>^ value)
        {
            this->stringsValue = value;

            // Populate the contained TextBox with the values
            // in the stringsValue array.
            StringBuilder^ sb =
                gcnew StringBuilder(this->stringsValue->Length);

            for (int i = 0; i < this->stringsValue->Length; i++)
            {
                sb->Append(this->stringsValue[i]);
                sb->Append(Environment::NewLine);
            }

            this->demoControlTextBox->Text = sb->ToString();
        }
    }
  1. 按下 F5 鍵以建置專案,並在 [使用者控制項測試容器] 中執行控制項。

  2. 在 [UserControl Test Container] 的 PropertyGrid 中尋找 Strings 屬性。 按一下 Strings 屬性,然後按一下省略號 (VisualStudioEllipsesButton 螢幕擷取畫面) 按鈕以開啟 [字串集合編輯器]。

  3. 在 [字串集合編輯器] 中輸入數個字串。 在每個字串的結尾按下 ENTER 鍵,藉此分隔各個字串。 當您輸入完字串後,請按一下 [確定]。

注意事項注意事項

   您輸入的字串會出現在 SerializationDemoControl 的 TextBox 中。

序列化集合屬性

若要測試控制項的序列化行為,請將控制項放置在表單上,並使用 [集合編輯器] 變更集合的內容。 您可藉由查看特殊的設計工具檔來檢視序列化集合狀態,[Windows Form 設計工具] 會對此設計工具檔發出程式碼。

若要序列化集合

  1. 將 Windows 應用程式專案加入至方案。 將專案命名為 SerializationDemoControlTest。

  2. 在 [工具箱] 中尋找名為 [SerializationDemoControlLib Components] 的索引標籤。 在此索引標籤中您會找到 SerializationDemoControl。 如需詳細資訊,請參閱逐步解說:自動將自訂元件填入工具箱

  3. 將 SerializationDemoControl 放置到表單上。

  4. 在 [屬性] 視窗中尋找 Strings 屬性。 按一下 Strings 屬性,然後按一下省略號 (VisualStudioEllipsesButton 螢幕擷取畫面) 按鈕以開啟 [字串集合編輯器]。

  5. 在 [字串集合編輯器] 中輸入數個字串。 在每個字串的結尾按下 ENTER 鍵,藉此分隔各個字串。 當您輸入完字串後,請按一下 [確定]。

注意事項注意事項

您輸入的字串會顯示在 SerializationDemoControl 的 TextBox 中。

  1. 在 [方案總管] 中按一下 [顯示所有檔案] 按鈕。

  2. 開啟 [Form1] 節點。 在該節點下方是稱為 [Form1.Designer.cs] 或 [Form1.Designer.vb] 的檔案。 [Windows Form 設計工具] 會將代表表單和子控制項設計階段狀態的程式碼發出至這個檔案。 在 [程式碼編輯器] 中開啟這個檔案。

  3. 開啟稱為 [Windows Form 設計工具產生的程式碼] 的區域,並尋找標記為 [serializationDemoControl1] 的區段。 在這個標籤底下,是代表控制項序列化狀態的程式碼。 您在步驟 5 輸入的字串會顯示於對 Strings 屬性的指派。 下列程式碼範例示範在輸入字串「red」、「orange」和「yellow」時會看見的類似程式碼。

  4. [Visual Basic]

    Me.serializationDemoControl1.Strings = New String() {"red", "orange", "yellow"}
    
  5. [C#]

    this.serializationDemoControl1.Strings = new string[] {
            "red",
            "orange",
            "yellow"};
    
  6. 在 [程式碼編輯器] 中,將 Strings 屬性上 DesignerSerializationVisibilityAttribute 的值變更為 Hidden

  7. [Visual Basic]

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    
  8. [C#]

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    
  9. 重新建置方案,並重複步驟 4 到 8。

注意事項注意事項

在這種情況下,[Windows Form 設計工具] 不會發出任何指派給 Strings 屬性。

後續步驟

一旦您知道如何序列化標準型別的集合,請考慮將自訂控制項更深入整合到設計階段環境中。 下列主題會描述如何增強自訂控制項的設計階段整合:

請參閱

工作

HOW TO:使用 DesignerSerializationVisibilityAttribute 序列化標準型別的集合

逐步解說:自動將自訂元件填入工具箱

參考

DesignerSerializationVisibilityAttribute

概念

設計工具序列化概觀