我的C#GUI中的MVC模式!?! [英] MVC-Pattern in my C# GUI !?!

查看:67
本文介绍了我的C#GUI中的MVC模式!?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好NG!

对于模式的学习工作,我想在我的

小应用程序中添加MVC模式。

On我的表单(查看)我有一个带有2个选项卡的TabController。在每个选项卡中,我都希望有一个数据网格,应该在一些更改后更新。所以这个两个数据网格必须实现我的观察者界面。我的班supportControl

将是模型,它从我的可观察类扩展。

这就是我理论上的理解......现在我对实现的问题
$ b C#中的$ b:


1.)2个Datagrids是必须更新的对象。我是否必须为
创建两个类(对于每个datagris一个),它从

System.Windows.Forms.DataGrid扩展并实现我的观察者界面?

2.)我在主表单上的DataGrid将由我创建的类初始化

在点1下。)???


对不起问题,但我不确定,如果这是正确的方式

实施MVC!


谢谢和问候


Marcel Hug

Hi NG !
For learning work with patterns I would like to add the MVC-Pattern in my
little application.
On my form (View) I have a TabController with 2 tabs. In each tab I would
like to have a datagrid, which should be updated after some changes. So this
two datagrids must implement my observer-interface. My class supportControl
would be the Model, which extends from my observable class.
Thats my theoretical understanding...now my questions to the implementation
in C#:

1.) The 2 Datagrids are the objects, which must be updated. Do I have to
create two classes (for each datagris one), which extends from
System.Windows.Forms.DataGrid and implements my observer interface ?
2.) My DataGrid on the main form would initialized by the classes i created
under point 1.) ???

Sorry this questions, but I''m not sure, if thats the right way of
implementation MVC !

Thanks and regards

Marcel Hug

推荐答案

Marcel,
Marcel,
对于有模式的学习工作,我想在我的
小应用程序中添加MVC模式。
我也尝试过实现MVC模式,但是我没有完全成功地获得


在我的表单上(查看)我有一个TabController与2个标签。在每个选项卡中,我都希望有一个数据网格,应该在一些更改后更新。所以这两个数据网格必须实现我的观察者界面。我的类supportControl
将是我的可观察类的模型。
这是我理论上的理解......现在我对C#中的实现提出了问题:
For learning work with patterns I would like to add the MVC-Pattern in my
little application. I have tried to implement the MVC-pattern as well, but I did not
completely succeed.
On my form (View) I have a TabController with 2 tabs. In each tab I would
like to have a datagrid, which should be updated after some changes. So this
two datagrids must implement my observer-interface. My class supportControl
would be the Model, which extends from my observable class.
Thats my theoretical understanding...now my questions to the implementation
in C#:


我做了如下:

1 /我创建表单并添加一些公共属性和事件。

属性用于设置和获取控件的数据

(文本框,数据网格,按钮标题)。这些事件用于

handeling按钮点击,数据更改等。

1 /我创建了另一个我称之为控制器类的类。这个类

是表单(我的GUI)和底层代码之间的连接。

这个类有像ShowUsers()这样的方法,它获取一个带有
来自数据层的用户数据,然后创建

用户表单的新实例,将数据设置为表单属性并显示表单。

这个类也从用户表单中捕获事件。


所以,举个例子,我在我的控制器上调用了ShowUsers()方法

等级。用户表单已通过此方法显示。现在我可以改变文本框和其他控件中的数据
。当我单击

按钮(例如保存按钮)时,控制器类将捕获该事件。它是
然后是处理其余部分的控制器类。控制器类

将从表单中获取调整后的数据,将其保存到数据库中,然后将
返回到表单中。

你也可以使用eventargs将数据从表单传递给

控制器类。


我希望你能理解我的意思我在做。这是我实现我的

类型的MVC模式的方式,我必须说它的效果非常好!


greetz,

Dries


I did it as follows:
1/ I create my forms and I add some public properties and events. The
properties are used for setting and getting the data for the controls
(textboxes, datagrids, button-captions). The events are used for
handeling button-clicks, data-changes, etc.
1/ I create another class which I call the controller-class. This class
is the connection between the forms (my GUI) and the underlying code.
This class has methods like ShowUsers(), which gets a datatable with the
user-data from the data-layers and then creates a new instance of the
Users-form, sets the data to the form-properties and shows the form.
This class is also catching the events from the User-form.

So, an example, I have called the ShowUsers() methode on my controller
class. The Users-form has been displayed by this method. Now I can
change the data in the textboxes and other controls. When I click a
button (e.g. a Save button), the controller class catches the event. It
is then the controller class that handles the rest. The controller class
will get the adapted data from the form, save it to the database and
then return the changed data to the form.

You can also use eventargs to pass data from the form to the
controller-class.

I hope you understand what I''m doing. This is the way I implement my
kind of MVC-pattern and I must say that it works very well!

greetz,
Dries


" Marcel Hug" <毫安****************** @ ATch.abb.com> aécritdansle message de

news: eB *** ***********@TK2MSFTNGP14.phx.gbl ...


|对于学习模式的工作,我想在我的

|中添加MVC模式很少申请。


我可以告诉你,你不能添加 MVC在一个应用程序中,你真的需要设计应用程序来使用MVC框架;但这可能仅仅是我们如何说同样的事情:-)


|在我的表单(视图)上,我有一个带有2个选项卡的TabController。在每个标签中,我将

|喜欢有一个datagrid,应该在一些更改后更新。所以

这个
|两个datagrids必须实现我的观察者界面。我的班级

supportControl

|将是我的可观察类中的模型。


您的表单上除了可视控件之外不应该有任何其他内容,

控制器应该是独立的类,在

表格之外的代码中实例化。


您还需要提供一组包装类,允许您

用于检测何时设置业务类的属性,以便

控制器可以处理诸如从

触发的更改或更改事件之类的事情。属性"


对于表单上的可视化组件,如果你想避免从现有的控件中获取实现Observer模式的

,这些也是需要

具有实现Observer模式的包装类,并对封闭的UI控件执行

Update方法。


您也可以使用现有控件的数据感知特性来替换它。
替换teh观察者模式,但是写一个控制器来捕获

控件的验证事件,这样你就可以在

离开编辑控件时强制执行业务规则。


IMO,MVC在新的.NET

数据感知控件中实现得相当好,尽管不是很明显。


我有在我的

网站上写了一系列关于MVP(一种超级MVC)的文章 www.carterconsulting.org.uk


乔安娜


-

乔安娜Carter [TeamB]

顾问软件工程师
"Marcel Hug" <ma******************@ATch.abb.com> a écrit dans le message de
news: eB**************@TK2MSFTNGP14.phx.gbl...

| For learning work with patterns I would like to add the MVC-Pattern in my
| little application.

May I advise that you can''t "add" MVC in an application, you really need to
design the application to use the MVC framework; but that may just be a
difference in how we say the same thing :-)

| On my form (View) I have a TabController with 2 tabs. In each tab I would
| like to have a datagrid, which should be updated after some changes. So
this
| two datagrids must implement my observer-interface. My class
supportControl
| would be the Model, which extends from my observable class.

You should not have anything other than visual controls on your forms, the
Controllers should be separate classes, instantiated in code outside of the
form.

You will also need to provide a set of wrapper classes that will allow you
to detect when you set the properties of your business classes, so that the
Controller can handle things like an Changing, or Changed event fired from
the "property".

As to the visual components on the form, if you want to avoid deriving from
the existing controls to implement the Observer pattern, these also need to
have wrapper classes that implement the Observer pattern and carry out the
Update method against the enclosed UI control.

You could also just use the data-aware nature of the existing controls to
replace teh Observer pattern, but write a Controller that catches the
Validating event of the controls so that you can enforce business rules when
leaving an edit control.

IMO, MVC is fairly well implemented, although not obviously, in the new .NET
data-aware controls.

I have written a series of articles on MVP (a sort of super MVC) on my
website www.carterconsulting.org.uk

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


Hello Marcel,
Hello Marcel,
嗨NG!小应用程序中添加MVC模式。
在我的表单(View)上我有一个带有2个选项卡的TabController。在每个标签中我都希望有一个数据网格,应该在一些更改后更新。
所以这两个数据网格必须实现我的观察者界面。我的班级
supportControl
将是我的可观察类扩展的模型。
Hi NG !
For learning work with patterns I would like to add the MVC-Pattern in
my
little application.
On my form (View) I have a TabController with 2 tabs. In each tab I
would
like to have a datagrid, which should be updated after some changes.
So this
two datagrids must implement my observer-interface. My class
supportControl
would be the Model, which extends from my observable class.




实际上,你既不需要观察者界面(C#是不是Java - 使用事件),

也不应该从任何特定的类扩展你的模型。您的控制器

应该更新模型,模型应该公开事件以指示状态变化,

并且您的视图应该订阅他们关心的那些模型事件。请参阅
http://www.codeproject.com/csharp/mo ... controller.asp 的介绍。


干杯,

-

Joerg Jooss
ne********@joergjooss.de



Actually, you need neither an observer interface (C# is not Java -- use events),
nor should your model need to extend from any particular class. Your controller
should update the model, the model should expose events to signal state changes,
and your views should subscribe to those model events they care about. See
http://www.codeproject.com/csharp/mo...controller.asp for an introduction.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de


这篇关于我的C#GUI中的MVC模式!?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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