연습: DesignerSerializationVisibilityAttribute를 사용하여 표준 형식의 컬렉션 serialize

사용자 지정 컨트롤에서 컬렉션을 속성으로 노출하는 경우도 있습니다. 이 연습에서는 DesignerSerializationVisibilityAttribute 클래스를 사용하여 디자인 타임에 컬렉션이 serialize되는 방법을 제어하는 방법에 대해 설명합니다. Content 값을 컬렉션 속성에 적용하면 속성이 serialize됩니다.

이 항목의 코드를 단일 목록으로 복사하려면 방법: DesignerSerializationVisibilityAttribute가 있는 표준 형식 컬렉션 serialize를 참조하십시오.

참고

표시되는 대화 상자와 메뉴 명령은 활성 설정이나 버전에 따라 도움말에서 설명하는 것과 다를 수 있습니다. 설정을 변경하려면 도구 메뉴에서 설정 가져오기 및 내보내기를 선택합니다. 자세한 내용은 설정에 대한 작업을 참조하십시오.

사전 요구 사항

이 연습을 완료하려면 다음과 같은 요건이 필요합니다.

  • Visual Studio가 설치된 컴퓨터에서 Windows Forms 응용 프로그램 프로젝트를 만들고 실행할 수 있는 권한

Serializable 컬렉션이 있는 컨트롤 만들기

첫 번째 단계는 serializable 컬렉션을 속성으로 갖는 컨트롤을 만드는 것입니다. 속성 창에서 액세스할 수 있는 컬렉션 편집기를 사용하여 이 컬렉션의 내용을 편집할 수 있습니다.

serializable 컬렉션이 있는 컨트롤을 만들려면

  1. SerializationDemoControlLib라는 Windows 컨트롤 라이브러리 프로젝트를 만듭니다. 자세한 내용은 Windows Control Library Template을 참조하십시오.

  2. UserControl1의 이름을 SerializationDemoControl로 변경합니다. 자세한 내용은 How to: Rename Identifiers를 참조하십시오.

  3. 속성 창에서 Padding.All 속성 값을 10으로 설정합니다.

  4. TextBox 컨트롤을 SerializationDemoControl에 배치합니다.

  5. TextBox 컨트롤을 선택합니다. 속성 창에서 다음 속성을 설정합니다.

    Property

    다음으로 변경

    Multiline

    true

    Dock

    Fill

    ScrollBars

    Vertical

    ReadOnly

    true

  6. 코드 편집기에서 stringsValue라는 문자열 배열 필드를 SerializationDemoControl에 선언합니다.

    ' 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 값은 컬렉션의 serialization을 활성화하는 데 사용됩니다.

' 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 키를 눌러 프로젝트를 빌드하고 UserControl Test Container에서 컨트롤을 실행합니다.

  2. UserControl Test ContainerPropertyGrid에서 Strings 속성을 찾습니다. Strings 속성을 클릭한 다음 줄임표(VisualStudioEllipsesButton 스크린 샷) 단추를 클릭하여 문자열 컬렉션 편집기를 엽니다.

  3. 문자열 컬렉션 편집기에서 여러 문자열을 입력합니다. 각 문자열의 끝에서 Enter 키를 눌러 문자열을 구분합니다. 문자열 입력이 완료되면 확인을 클릭합니다.

참고

   입력한 문자열이 SerializationDemoControl의 TextBox에 표시됩니다.

컬렉션 속성 Serialize

컨트롤의 serialization 동작을 테스트하려면 해당 컬렉션을 폼에 배치하고 컬렉션 편집기를 사용하여 컬렉션의 내용을 변경합니다. Windows Forms 디자이너에서 코드를 생성하는 특수 디자이너 파일을 조사하여 serialize된 컬렉션의 상태를 확인할 수 있습니다.

컬렉션을 serialize하려면

  1. 솔루션에 Windows 응용 프로그램 프로젝트를 추가합니다. 프로젝트 이름을 SerializationDemoControlTest로 지정합니다.

  2. 도구 상자에서 SerializationDemoControlLib 구성 요소라는 탭을 찾습니다. 이 탭에서 SerializationDemoControl을 찾습니다. 자세한 내용은 연습: 도구 상자에 자동으로 사용자 지정 구성 요소 채우기를 참조하십시오.

  3. SerializationDemoControl을 폼에 배치합니다.

  4. 속성 창에서 Strings 속성을 찾습니다. Strings 속성을 클릭한 다음 줄임표(VisualStudioEllipsesButton 스크린 샷) 단추를 클릭하여 문자열 컬렉션 편집기를 엽니다.

  5. 문자열 컬렉션 편집기에서 여러 문자열을 입력합니다. 각 문자열의 끝에서 Enter 키를 눌러 문자열을 구분합니다. 문자열 입력이 완료되면 확인을 클릭합니다.

참고

입력한 문자열이 SerializationDemoControl의 TextBox에 표시됩니다.

  1. 솔루션 탐색기에서 모든 파일 표시 단추를 클릭합니다.

  2. Form1 노드를 엽니다. 이 노드 아래에 Form1.Designer.cs 또는 Form1.Designer.vb라는 파일이 있습니다. 이 파일은 Windows Forms 디자이너에서 폼과 해당 자식 컨트롤의 디자인 타임 상태를 나타내는 코드를 생성하는 파일입니다. 코드 편집기에서 이 파일을 엽니다.

  3. Windows Form 디자이너에서 생성한 코드라는 영역을 열고 serializationDemoControl1이라는 레이블이 지정된 섹션을 찾습니다. 이 레이블 아래에는 컨트롤의 serialize된 상태를 나타내는 코드가 있습니다. 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 Forms 디자이너는 Strings 속성에 아무 것도 할당하지 않습니다.

다음 단계

표준 형식 컬렉션을 serialize하는 방법을 알고 있는 경우 사용자 지정 컨트롤을 디자인 타임 환경에 보다 세부적으로 통합해 보십시오. 다음 항목에서는 사용자 지정 컨트롤의 디자인 타임 통합을 향상시키는 방법에 대해 설명합니다.

참고 항목

작업

방법: DesignerSerializationVisibilityAttribute가 있는 표준 형식 컬렉션 serialize

연습: 도구 상자에 자동으로 사용자 지정 구성 요소 채우기

참조

DesignerSerializationVisibilityAttribute

개념

디자이너 serialization 개요