Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#Winforms中正确实现被动的模型视图演示器?_C#_Design Patterns_Mvp_Passive View - Fatal编程技术网

如何在C#Winforms中正确实现被动的模型视图演示器?

如何在C#Winforms中正确实现被动的模型视图演示器?,c#,design-patterns,mvp,passive-view,C#,Design Patterns,Mvp,Passive View,我现在正在研究设计模式,我对这个模型视图演示者是相当陌生的,虽然我已经有asp.net mvc的经验,但我正在尝试在winforms中实现mvp 文本框中的字符串将使用基于组合框的算法进行排序。现在,当我单击按钮时,它抛出一个空引用异常 以下是用户界面: 以下是我的课程和代码: class FormPresenter { private ISortingView _view; private string _algorith

我现在正在研究设计模式,我对这个模型视图演示者是相当陌生的,虽然我已经有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());


            }
        }
类FormPresenter
{
私有ISortingView\u视图;
私有字符串算法;
私有StringToSortModel sortMe=新StringToSortModel();
public FormPresenter(ISortingView视图)
{
_视图=视图;
_view.sort字符串+=视图\u sort字符串;
sortMe.sorttthis=view.stringToSort;
_算法=_view.algorithm;
//算法=view.stringToSort;
//sortingform.sortthesting+=(对象)
}
私有无效视图\u排序字符串(对象发送方,事件参数e)
{
SortContext上下文=新的SortContext();
_view.sortedText=context.Sort(sortMe.sorttthis.ToCharArray());
}
}
接口ISortingView
{
事件处理程序排序字符串;
字符串stringToSort{get;}
字符串算法{get;}
字符串排序文本{get;set;}
}
公共部分类排序窗体:窗体,ISortingView
{
公共分类表()
{
初始化组件();
comboBox1.Items.Add(“气泡排序”);
comboBox1.Items.Add(“插入排序”);
comboBox1.SelectedItem=“气泡排序”;
textBox1.Text=“Emiri”;
}
公共事件事件处理程序排序;
公共字符串算法{get{return comboBox1.SelectedItem.ToString();}}
公共字符串stringToSort{get{return textBox1.Text;}}
公共字符串sortedText{get{return label2.Text;}set{label2.Text=value;}}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//char[]x=textBox1.Text.ToCharArray();
//SortContext con=新的SortContext();
//con.SetSortStrategy(新的InsertionSort());
//label2.Text=con.Sort(x);
//if(排序字符串!=null)
//{
//这将提示一个空异常错误
Sortthesting(发送方,e);
//}
}
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var mainForm=new SortingForm();
var presenter=新表单presenter(mainForm);
运行(新的SortingForm());
}
}
我没有包括模型的代码和包含排序函数的类,以保持这篇文章的简短。我的问题是,当单击按钮时,它会抛出一个空引用异常错误,我已经坚持了几个小时了


先生/女士,您的回答将非常有帮助。谢谢您+++

您的空值来自此行

sortTheString(sender, e);
因为您在演示者中使用的表单实例不同。请在主窗体中更改为此

Application.Run(mainForm);

事件处理程序没有任何订阅服务器(因为
应用程序。Run(new SortingForm());
C#将其视为null,而不是空的订阅服务器列表。

我的最佳选择是从未提供代码的
sortTheString
函数中引发异常
ISortingView mainForm = new SortingForm();
var presenter = new FormPresenter(mainForm);
Application.Run(mainForm as Form);