繁体   English   中英

如何在C#Winforms中使用被动方式正确实现模型视图呈现器?

[英]How do I properly implement a model-view-presenter with passive in C# Winforms?

我现在正在研究设计模式,虽然对asp.net mvc已有一定的经验,但我对这个模型视图演示者还是相当陌生,我正在尝试在winforms中实现mvp。

文本框中的字符串将使用基于组合框的算法进行排序。 现在,当我单击按钮时,它将引发空引用异常

这是用户界面: 在此处输入图片说明

这是我的课程和代码:

    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());


            }
        }

我没有包括该模型的代码和包含排序功能的类,这些内容使本文简短。 我的问题是,单击按钮时会引发空引用异常错误,这是我已经坚持了好几个小时了。

先生/女士,您的回答会很有帮助。 谢谢++

您的null来自此行

sortTheString(sender, e);

因为您不在Presenter中使用相同的表单实例。 更改为您的主要...

Application.Run(mainForm);

事件处理程序没有任何订阅者(由于Application.Run(new SortingForm()); C#将其视为null而不是空的订阅者列表)。

ISortingView mainForm = new SortingForm();
var presenter = new FormPresenter(mainForm);
Application.Run(mainForm as Form);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM