2013-02-25 46 views
0

我正在學習設計模式,我對這個模型 - 視圖 - 主持人相當新,雖然我已經在asp.net mvc的經驗,我試圖在winforms中執行mvp。如何在C#Winforms中正確實現模型 - 視圖 - 呈現器的被動模式?

文本框中的字符串將使用基於組合框的算法進行排序。當我點擊按鈕,現在,它拋出一個空引用異常

這裏是UI: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()); 


      } 
     } 

我還沒有爲模型的代碼以及包含排序功能的類來保持這篇短文。我遇到的問題是,當按鈕被點擊時,它會拋出一個空引用異常錯誤,這是我已經堅持了幾個小時了。

先生/女士您的回答會有很大的幫助。謝謝++

+0

我最好的賭注是異常是從你沒有提供代碼的'sortTheString'函數中拋出的 – 2013-02-25 06:25:44

回答

1

,因爲你是不是在你的演示使用相同的形式比如你空從該行

sortTheString(sender, e); 

到來。更改爲這個在主...

Application.Run(mainForm); 

事件處理程序沒有任何用戶(因爲Application.Run(new SortingForm()); C#將可以把它看成空,而不是一個空的用戶列表。

0
ISortingView mainForm = new SortingForm(); 
var presenter = new FormPresenter(mainForm); 
Application.Run(mainForm as Form); 
相關問題