C#Windows Forms App:将GUI与Business Logic分开 [英] C# Windows Forms App: Separate GUI from Business Logic

查看:165
本文介绍了C#Windows Forms App:将GUI与Business Logic分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一些关于如何在一个简单的C#Windows窗体应用程序中分离UI和业务逻辑的建议。



我们来看这个例子:



UI由一个简单的文本框和一个按钮组成。用户输入0到9之间的数字,然后点击按钮。该程序应该添加10个数字,并更新该值的文本框。





业务逻辑部分应该不了解UI。这怎么可以实现?



这是空的Process类(Business Logic):

 命名空间addTen 
{
class Process
{
public int AddTen(int num)
{
return num + 10;
}
}
}

要求是: p>


  1. 当用户点击按钮,不知何故,Process :: AddTen被调用。

  2. 文本框必须使用Process :: AddTen的返回值进行更新。

我只是不知道如何连接这两个。 p>

解决方案

首先,你需要改变你的班级名称。 进程是类库中的类的名称,并且可能会导致任何人读取代码的混乱。我们假设,对于这个答案的其余部分,您将类名更改为 MyProcessor (仍然是一个不好的名字,但不是一个好的 - 已知的,经常使用的类。)



此外,您缺少要检查的代码,以确保用户输入确实是0到9之间的数字。在表单的代码而不是类代码中,这是适当的。




  • 假设TextBox被命名为textBox1(添加了第一个TextBox的VS生成的默认值进一步假设按钮的名称是button1



在Visual Studio中,双点击按钮来创建按钮单击事件处理程序,它将如下所示:

  protected void button1_Click(object sender, EventArgs e)
{

}

在事件处理程序,添加代码,如下所示:

  protected void button1_Click(object sender,Even tArgs e)
{
int safelyConvertedValue = -1;
if(!System.Int32.TryParse(textBox1.Text,out safelyConvertedValue))
{
//根本输入不是有效的整数值。
MessageBox.Show(你需要输入1到9之间的数字);
//中止处理。
return;
}

//如果你这么做,TryParse函数应该将
//名为safelyConvertedValue的变量的值设置为在TextBox中输入的值。
//但是,它仍然可能在0-9的允许范围之内
if(safelyConvertedValue< 0 || safelyConvertedValue> 9)
{
//输入不在指定范围内。
MessageBox.Show(你需要输入1到9之间的数字);
//中止处理。
return;
}

MyProcessor p = new MyProcessor();
textBox1.Text = p.AddTen(safelyConvertedValue).ToString();
}






访问修饰符设置正确,应该如下所示:

 命名空间addTen 
{
public class MyProcessor
{
public int AddTen(int num)
{
return num + 10;
}
}
}


I would like some advice on how to separate the UI and business logic in a simple C# Windows Forms Application.

Let's take this example:

The UI consists of a simple textbox and a button. The user enters a number between 0 and 9 and clicks the button. The program should add 10 to the number and update the text box with that value.

The business logic part should have no idea of the UI. How can this be accomplished?

Here's the empty Process class (Business Logic):

namespace addTen
{
    class Process
    {
        public int AddTen(int num)
        {
            return num + 10;
        }
    }
}

The requirement is:

  1. When the user clicks the button, somehow, the Process::AddTen gets invoked.
  2. The Textbox must be updated with the return value of Process::AddTen.

I just don't know how to connect these two.

解决方案

First, you need to change your class name. "Process" is name of a class in the Class Library and will likely cause confusion for anyone reading your code.

Let's assume, for the rest of this answer that you changed the class name to MyProcessor (still a bad name, but not a well-known, often-used class.)

Also, you're missing the code to check to ensure that the user input is, indeed, a number between 0 and 9. That's appropriate in the Form's code rather than the class code.

  • Assuming the TextBox is named textBox1 (The VS generated default for the first TextBox added to the form)
  • Further assuming the button's name is button1

In Visual Studio, double-click on the button to create the button click event handler, which will look like this:

protected void button1_Click(object sender, EventArgs e)
{

}

Within the event handler, add code so it looks like this:

 protected void button1_Click(object sender, EventArgs e)
 {
   int safelyConvertedValue = -1;
   if(!System.Int32.TryParse(textBox1.Text, out safelyConvertedValue))
   {
     // The input is not a valid Integer value at all.
     MessageBox.Show("You need to enter a number between 1 an 9");
     // Abort processing.
     return;
   }

   // If you made it this far, the TryParse function should have set the value of the 
   // the variable named safelyConvertedValue to the value entered in the TextBox.
   // However, it may still be out of the allowable range of 0-9)
   if(safelyConvertedValue < 0 || safelyConvertedValue > 9)
   {
     // The input is not within the specified range.
     MessageBox.Show("You need to enter a number between 1 an 9");
     // Abort processing.
     return;
   }

   MyProcessor p = new MyProcessor();
   textBox1.Text = p.AddTen(safelyConvertedValue).ToString();
 }


The class, with the access modifier set properly, should look like this:

namespace addTen       
{       
    public class MyProcessor
    {       
        public int AddTen(int num)       
        {       
            return num + 10;       
        }       
    }       
}    

这篇关于C#Windows Forms App:将GUI与Business Logic分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆