2013-02-25 1 views
0

디자인 패턴을 지금 공부하고 있는데, 비록 내가 asp.net MVC에서 이미 경험이 있지만,이 모델 뷰 프리젠터에 상당히 익숙하다. winforms에서 mvp의 구현을 시도하고있다.C# Winforms에서 수동으로 모델보기 표시기를 올바르게 구현하려면 어떻게해야합니까?

텍스트 상자의 문자열은 콤보 박스를 기반으로 알고리즘으로 정렬됩니다. 여기 enter image description here

내 클래스와 코드입니다 :

class FormPresenter 
     { 
      private ISortingView _view; 
      private string _algorithm; 
      private StringToSortModel sortMe = new StringToSortModel(); 

      public FormPresenter(ISortingView view) 
      { 
       _view = view; 
       _view.sortTheString += view_sortString; 
       sortMe.sortThis = view.stringToSort; 
       _algorithm = _view.algorithm; 
       //Algorithm = view.stringToSort; 
       //sortingform.sortTheString += (obj 
      } 

      private void view_sortString(object sender, EventArgs e) 
      { 

       SortContext context = new SortContext(); 
       _view.sortedText = context.Sort(sortMe.sortThis.ToCharArray()); 


      } 

     } 


interface ISortingView 
    { 
     event EventHandler sortTheString; 
     string stringToSort { get; } 
     string algorithm { get; } 
     string sortedText { get; set; } 

    } 


    public partial class SortingForm : Form, ISortingView 
     { 
      public SortingForm() 
      { 
       InitializeComponent(); 
       comboBox1.Items.Add("Bubble Sort"); 
       comboBox1.Items.Add("Insertion Sort"); 
       comboBox1.SelectedItem = "Bubble Sort"; 
       textBox1.Text = "Emiri"; 
      } 


      public event EventHandler sortTheString; 
      public string algorithm { get { return comboBox1.SelectedItem.ToString(); } } 
      public string stringToSort { get { return textBox1.Text; } } 
      public string sortedText { get { return label2.Text; } set { label2.Text = value; } } 




      private void Form1_Load(object sender, EventArgs e) 
      { 

      } 


      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
      { 

      } 

      private void button1_Click(object sender, EventArgs e) 
      { 
       //char[] x = textBox1.Text.ToCharArray(); 
       //SortContext con = new SortContext(); 
       //con.SetSortStrategy(new InsertionSort()); 
       //label2.Text = con.Sort(x); 
       //if(sortString != null) 
       //{ 

       //this prodcues a null exception error 
       sortTheString(sender, e); 


       //} 



      } 

    static class Program 
     { 
      /// <summary> 
      /// The main entry point for the application. 
      /// </summary> 
      [STAThread] 
      static void Main() 
      { 
       Application.EnableVisualStyles(); 
       Application.SetCompatibleTextRenderingDefault(false); 
       var mainForm = new SortingForm(); 
       var presenter = new FormPresenter(mainForm); 
       Application.Run(new SortingForm()); 


      } 
     } 

내가 모델의 코드를 포함하지 않은 지금 내가 버튼을 클릭하면 그것은 UI를 널 참조 예외 여기

입니다 던졌습니다 클래스에는이 게시물을 짧게 유지하는 정렬 함수가 들어 있습니다. 내가 가진 문제는 버튼을 클릭하면 null 참조 예외 오류가 발생하는 것입니다, 내가 이미 몇 시간 동안 붙어있다.

귀하의 답변은 큰 도움이 될 것입니다. 당신이 당신의 발표자에 같은 형태의 인스턴스를 사용하지 않기 때문에 귀하의 널 (null)이 줄

sortTheString(sender, e); 

에서 오는

+0

최선을 당신이 코드를 제공하지 않은'sortTheString' 함수 내에서 예외가 던져지는 것이 겠지요. –

답변

1

를 ++ 감사드립니다. Application.Run(new SortingForm()); C#을 오히려 빈 가입자 목록 이상에 null 것으로 취급하기 때문에 이벤트 핸들러 (모든 가입자가없는

Application.Run(mainForm); 

... 메인이로 변경합니다.

0
ISortingView mainForm = new SortingForm(); 
var presenter = new FormPresenter(mainForm); 
Application.Run(mainForm as Form); 
관련 문제