2010-11-29 3 views
2

두 개의 사용자 정의 컨트롤이 있는데 하나는 확인란이있는 간단한 그림 홀더입니다. 그리고 이전 컨트롤의 컬렉션을 가진 것보다 컨테이너 역할을하는 또 하나. 사용자 정의 컨트롤을 디자인 뷰로 끌면 Visual Studio에서 오류가 발생합니다.

은 그래서 HorizontalPictureScroller 많은 SelectablePicture 컨트롤을 할 수 있습니다. 나는 각 컨트롤에 대한 작은 코드를 붙여 것이다 :

먼저 HorizontalPictureScroller : 이제

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Collections.ObjectModel; 

namespace WinformsPlayground 
{ 
    [Serializable()] 
    public partial class HorizontalPictureScroller : UserControl 
    { 
     public HorizontalPictureScroller() 
     { 
      InitializeComponent(); 
      Pictures = new ObservableCollection<SelectablePicture>(); 
      Pictures.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Pictures_CollectionChanged); 
     }  

     #region "Properties" 
     public ObservableCollection<SelectablePicture> Pictures { get; set; } 
     private int PositionControlX = 0; 
     #endregion 

     #region "Methods" 
     private void RedrawPictures() 
     { 
      PositionControlX = 0; 

      foreach (var picture in Pictures) 
      { 
       picture.Location = new Point(PositionControlX + panelPicturesWrapper.AutoScrollPosition.X, 0); 
       PositionControlX += 130; 
       panelPicturesWrapper.Controls.Add(picture); 
      } 
     } 

     public void AddPicture(SelectablePicture picture) 
     { 
      Pictures.Add(picture); 
     } 

     public void RemovePicture(SelectablePicture picture) 
     { 
      Pictures.Remove(picture); 
     } 

     public void MovePictureLeft(int index) 
     { 
      SelectablePicture tmpPicture = Pictures[index]; 
      Pictures[index] = Pictures[index - 1]; 
      Pictures[index - 1] = tmpPicture; 
     } 

     public void MovePictureRight(int index) 
     { 
      SelectablePicture tmpPicture = Pictures[index]; 
      Pictures[index] = Pictures[index + 1]; 
      Pictures[index + 1] = tmpPicture; 
     } 
     #endregion 

     #region "Events" 
     void Pictures_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
     { 
      RedrawPictures(); 
     }   
     #endregion 
    } 
} 

SelectablePicture 제어 : 여기

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WinformsPlayground 
{ 
    [Serializable()] 
    public partial class SelectablePicture : UserControl 
    { 
     public SelectablePicture() 
     { 
      InitializeComponent(); 
      panel1.BackgroundImageLayout = ImageLayout.Zoom; 
     } 

     public SelectablePicture(Image image) 
     { 
      panel1.BackgroundImage = image; 
      panel1.BackgroundImageLayout = ImageLayout.Zoom; 
     } 

     #region "Properties" 
     public Image Image() 
     { 
      return panel1.BackgroundImage; 
     } 

     public bool IsSelected() 
     { 
      return chkSelected.Checked; 
     } 
     #endregion 

     #region "Methods" 
     public void ToggleCheckBox() 
     { 
      chkSelected.Checked = chkSelected.Checked ? false : true; 
     } 

     public void VisuallySelect() 
     { 
      this.BackColor = Color.FromArgb(51, 153, 255); 
     } 

     public void VisuallyDeselect() 
     { 
      //If none of the controls inside the usercontrol have focus, set this control to white. 
      if (!this.Focused && !this.panel1.Focused && !this.chkSelected.Focused) 
      { 
       this.BackColor = Color.White; 
      } 
     }   
     #endregion 

     #region "Events" 
     private void panel1_Click(object sender, EventArgs e) 
     { 
      VisuallySelect(); 
      ToggleCheckBox(); 
      panel1.Focus(); 
     } 

     private void chkSelected_Click(object sender, EventArgs e) 
     { 
      VisuallySelect(); 
      ToggleCheckBox(); 
      chkSelected.Focus(); 
     } 

     private void SelectablePicture_Click(object sender, EventArgs e) 
     { 
      VisuallySelect(); 
      ToggleCheckBox(); 
      this.Focus(); 
     } 

     private void panel1_Leave(object sender, EventArgs e) 
     { 
      VisuallyDeselect(); 
     } 

     private void chkSelected_Leave(object sender, EventArgs e) 
     { 
      VisuallyDeselect(); 
     } 

     private void SelectablePicture_Leave(object sender, EventArgs e) 
     { 
      VisuallyDeselect(); 
     } 
     #endregion   
    } 
} 

은의 스크린 샷입니다 HorizontalPictureScroller를 Winform 디자인보기로 드래그하려고 할 때 오류가 발생합니다 (미안 텍스트를 여기에 붙여 넣을 수 없습니다) : alt text

내 사용자 컨트롤은 매우 간단하며 코드에서 어떤 문제가 있는지 알 수 없습니다.

어쩌면 그것은 내 맹신적 인 실수 일 수 있습니다. : P 시간 내 주셔서 감사합니다.

답변

1

SerializableAttribute을 사용하고 있지만 UserControl을 사용하고 있지 않으므로 예외가 발생합니다. SerializableAttribute에 대한 문서에서

:

객체의 그래프에서 모든 유형이 적용된 SerializableAttribute 특성이없는 직렬화되는 경우 공용 언어 런타임 SerializationException가 발생합니다.

+0

[Serializable()]의 제목을 제거하더라도 여전히 오류가 발생합니다. 실제로 누군가가 오류의 원인이라고 말했기 때문에 나는 그것을 추가했습니다 : P 다른 아이디어? 감사! –

+0

@Serg,'[Serializable()]'을 제거하고 각 컨트롤을 개별적으로 추가 할 때 둘 다 예외가 발생합니까? 'HorizontalPictureScroller' 만 예외를 던지면 문제는 'Serializable'로 표시되어 사용하는 ObservableCollection 일 수 있습니다. –

+0

@adrift : 예! HorizontalPictureScroller 만 디자이너 뷰로 끌면 예외가 발생합니다. 이 문제를 해결하는 방법에 대한 제안 사항이 있으십니까? –

0

UserControl SelectablePicture는 컬렉션에서 사용되기 때문에 직렬화해야하는 것처럼 보입니다. 그래서 당신은 속성 [Serializable()]을 가진 해결책에서 가까웠습니다. 하지만 당신은 더 나은이이 문제를 해결하는 첫 번째 단계는 당신의 UserControl을 SelectablePicture

public partial class SelectablePicture : UserControl, ISerializable 
{ 
    #region ISerializable Membres 

    public void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     ... 
    } 

    #endregion 
} 

의를 ISerializable 인터페이스를 구현하는 것입니다. 그것으로 당신은 UserControl을 드래그 - 드롭 할 때 오류 메시지가 발생하지 않아야합니다. 하지만 이제는 직렬화를 관리해야합니다.

누군가 직렬화가 필요한 이유를 알고 있다면 관심이 있습니다.

관련 문제